2016-11-09 18:14:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/graphics/traveler/tools/libwld/wld_loadpaltable.c
|
|
|
|
* This file contains the logic that creates the range palette table that is
|
|
|
|
* used to modify the palette with range to hit
|
|
|
|
*
|
|
|
|
* Copyright (C) 2016 Gregory Nutt. All rights reserved.
|
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Included files
|
|
|
|
*************************************************************************/
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
/* If the following switch is false, then the whole g_pal_table is loaded from
|
|
|
|
* the file. Otherwise, the g_pal_table will be calculated from a range
|
2016-11-09 18:14:35 +01:00
|
|
|
* table
|
|
|
|
*/
|
|
|
|
|
2016-11-09 21:12:21 +01:00
|
|
|
#define USE_PAL_RANGES true
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
#include "trv_types.h"
|
|
|
|
#include "wld_world.h"
|
|
|
|
#include "wld_paltable.h"
|
|
|
|
#if (!MSWINDOWS)
|
2016-11-10 19:21:47 +01:00
|
|
|
# include "wld_bitmaps.h"
|
|
|
|
# include "wld_color.h"
|
2016-11-09 18:14:35 +01:00
|
|
|
#endif
|
|
|
|
#include "wld_mem.h"
|
|
|
|
#include "wld_utils.h"
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/* This indicates the maximum number of palette ranges which may be
|
|
|
|
* specified in the file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if USE_PAL_RANGES
|
2016-11-10 19:21:47 +01:00
|
|
|
# define MAX_PAL_RANGES 64
|
2016-11-09 18:14:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 18:42:37 +01:00
|
|
|
* Private Type Definitions
|
2016-11-09 18:14:35 +01:00
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#if USE_PAL_RANGES
|
|
|
|
typedef struct
|
2016-11-10 19:21:47 +01:00
|
|
|
{
|
|
|
|
uint8_t firstColor;
|
|
|
|
int8_t colorRange;
|
|
|
|
uint8_t clipColor;
|
|
|
|
} pal_range_t;
|
2016-11-09 18:14:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Public Data
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/* This is the palette table which is used to adjust the texture values
|
|
|
|
* with distance
|
|
|
|
*/
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
trv_pixel_t *g_pal_table[NUM_ZONES];
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 18:42:37 +01:00
|
|
|
* Name: wld_allocate_paltable
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
static void wld_allocate_paltable(uint32_t palTabEntrySize)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NUM_ZONES; i++)
|
|
|
|
{
|
2016-11-10 19:21:47 +01:00
|
|
|
g_pal_table[i] =
|
|
|
|
(trv_pixel_t *) wld_malloc(palTabEntrySize * sizeof(trv_pixel_t));
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-09 19:29:30 +01:00
|
|
|
* Name: wld_load_paltable
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
2016-11-10 18:42:37 +01:00
|
|
|
* This function loads the g_pal_table from the specified file
|
2016-11-09 18:14:35 +01:00
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-09 19:29:30 +01:00
|
|
|
uint8_t wld_load_paltable(char *file)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
#if (!MSWINDOWS)
|
2016-11-10 18:42:37 +01:00
|
|
|
trv_pixel_t *palptr;
|
|
|
|
color_lum_t lum;
|
2016-11-10 19:21:47 +01:00
|
|
|
int16_t zone;
|
2016-11-09 18:14:35 +01:00
|
|
|
float scale;
|
|
|
|
int pixel;
|
|
|
|
|
|
|
|
/* Allocate memory to hold the palette range mapping data */
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
wld_allocate_paltable(TRV_PIXEL_MAX + 1);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* The first zone is just the identity transformation */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr = g_pal_table[0];
|
|
|
|
for (pixel = 0; pixel <= TRV_PIXEL_MAX; pixel++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
*palptr++ = pixel;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process each remaining distance zone */
|
|
|
|
|
|
|
|
for (zone = 1; zone < NUM_ZONES; zone++)
|
|
|
|
{
|
|
|
|
/* Point to the palette mapping data for this zone */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr = g_pal_table[zone];
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Calculate the luminance scaling factor to use for this zone */
|
|
|
|
|
|
|
|
scale = (float)(NUM_ZONES - zone) / (float)NUM_ZONES;
|
|
|
|
|
|
|
|
/* Scale each element of the palette */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
for (pixel = 0; pixel <= TRV_PIXEL_MAX; pixel++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
/* Get the unit vector + luminance representation of the pixel */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
wld_pixel2lum(pixel, &lum);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Re-scale the luminance component by range scale factor */
|
|
|
|
|
|
|
|
lum.luminance *= scale;
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
/* Then save the pixel associated with this scaled value in the range
|
|
|
|
* palette table */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
*palptr++ = wld_lum2pixel(&lum);
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-10 18:42:37 +01:00
|
|
|
|
2016-11-09 18:14:35 +01:00
|
|
|
return WORLD_SUCCESS;
|
|
|
|
|
|
|
|
#else
|
2016-11-10 19:21:47 +01:00
|
|
|
# if USE_PAL_RANGES
|
|
|
|
FILE *fp, *fopen();
|
|
|
|
int16_t i;
|
|
|
|
int16_t nranges;
|
|
|
|
int16_t zone;
|
|
|
|
int16_t palndx;
|
2016-11-10 18:42:37 +01:00
|
|
|
trv_pixel_t plotcolor;
|
|
|
|
trv_pixel_t *palptr;
|
|
|
|
pal_range_t ranges[MAX_PAL_RANGES];
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Open the file which contains the palette table */
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
2016-11-10 19:21:47 +01:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return PALR_FILE_OPEN_ERROR;
|
|
|
|
}
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Read the number of ranges from the file */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
nranges = wld_read_decimal(fp);
|
|
|
|
if (nranges > MAX_PAL_RANGES)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
fclose(fp);
|
|
|
|
return PALR_TOO_MANY_RANGES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then read all of the palette range data from the file */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
for (i = 0; i < nranges; i++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-09 19:29:30 +01:00
|
|
|
ranges[i].firstColor = wld_read_decimal(fp);
|
|
|
|
ranges[i].colorRange = wld_read_decimal(fp);
|
2016-11-10 19:21:47 +01:00
|
|
|
ranges[i].clipColor = wld_read_decimal(fp);
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We are now done with the input file */
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
|
|
|
|
/* Allocate memory to hold the palette range mapping data */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
wld_allocate_paltable(PALETTE_SIZE);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Process each distance zone */
|
|
|
|
|
|
|
|
for (zone = 0; zone < NUM_ZONES; zone++)
|
|
|
|
{
|
|
|
|
/* Point to the palette mapping data for this zone */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr = g_pal_table[zone];
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Process each possible palette index within the zone */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
for (palndx = 0; palndx < PALETTE_SIZE; palndx++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 19:21:47 +01:00
|
|
|
/* Assume that the range will not be found. In this case, we will
|
|
|
|
* perform the 1-to-1 mapping */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor = palndx;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Find the range the color is in. */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
for (i = 0; i < nranges; i++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
/* Check if the color range is ascending or descending */
|
|
|
|
|
|
|
|
if (ranges[i].colorRange < 0)
|
|
|
|
{
|
|
|
|
/* The colors are descending */
|
2016-11-10 18:42:37 +01:00
|
|
|
if ((palndx <= ranges[i].firstColor) &&
|
|
|
|
(palndx > (ranges[i].firstColor + ranges[i].colorRange)))
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
/* Found it, set the new plotcolor */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
if (plotcolor > zone)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
/* Offset the color by the zone we are processing */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor -= zone;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
/* Check if we have exceeded the range of this color.
|
|
|
|
* If so, then set the color to the clipColor */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
if (plotcolor <=
|
|
|
|
ranges[i].firstColor + ranges[i].colorRange)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor = ranges[i].clipColor;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor = ranges[i].clipColor;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now break out of the loop */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The colors are ascending */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
else if ((palndx >= ranges[i].firstColor) &&
|
|
|
|
(palndx < (ranges[i].firstColor + ranges[i].colorRange)))
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
/* Found it, set the new plotcolor */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor += zone;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Check if we have exceeded the range of this color. If so,
|
2016-11-10 19:21:47 +01:00
|
|
|
* then set the color to black */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
if (plotcolor >= ranges[i].firstColor + ranges[i].colorRange)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
plotcolor = ranges[i].clipColor;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Now break out of the loop */
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
/* Save the plotcolor in the g_pal_table */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr[palndx] = plotcolor;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return WORLD_SUCCESS;
|
2016-11-10 19:21:47 +01:00
|
|
|
# else
|
2016-11-10 18:42:37 +01:00
|
|
|
FILE *fp;
|
|
|
|
int16_t zone;
|
|
|
|
int16_t palndx;
|
|
|
|
uint8_t plotcolor;
|
|
|
|
uint8_t *palptr;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Allocate memory to hold the palette range mapping data */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
wld_allocate_paltable();
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Open the file which contains the palette table */
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
2016-11-10 19:21:47 +01:00
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return PALR_FILE_OPEN_ERROR;
|
|
|
|
}
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Process each distance zone */
|
|
|
|
|
|
|
|
for (zone = 0; zone < NUM_ZONES; zone++)
|
|
|
|
{
|
|
|
|
/* Point to the palette mapping data for this zone */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr = GET_PALPTR(zone);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Process each possible palette index within the zone */
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
for (palndx = 0; palndx < PALETTE_SIZE; palndx++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
/* Read the data into g_pal_table */
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
palptr[palndx] = wld_read_decimal(fp);
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return WORLD_SUCESS;
|
2016-11-10 19:21:47 +01:00
|
|
|
# endif /* USE_PAL_RANGES */
|
|
|
|
#endif /* !MSWINDOWS */
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|