2016-11-09 18:14:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/graphics/traveler/tools/libwld/wld_readgraphicfile.c
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "trv_types.h"
|
|
|
|
#include "wld_mem.h"
|
|
|
|
#include "wld_bitmaps.h"
|
|
|
|
#include "wld_graphicfile.h"
|
|
|
|
#include "wld_pcx.h"
|
|
|
|
#include "wld_utils.h"
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
graphic_file_t *wld_LoadPPM(FILE * fp, char *filename);
|
|
|
|
graphic_file_t *wld_LoadGIF(FILE * fp, char *filename);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Private Data
|
|
|
|
*************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static const char g_pcx_extension[] = ".PCX";
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 23:20:35 +01:00
|
|
|
* Name: wld_check_format
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static graphic_file_format_t wld_check_format(FILE * fp, char *filename)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
char magic[MAGIC_LENGTH];
|
|
|
|
|
|
|
|
if (fread(magic, 1, MAGIC_LENGTH, fp) != MAGIC_LENGTH)
|
|
|
|
{
|
2016-11-11 21:17:05 +01:00
|
|
|
wld_fatal_error("ERROR: Reading texture %s.", filename);
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
if (strncmp(magic, PPM_MAGIC, sizeof(PPM_MAGIC) - 1) == 0)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_PPM;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
else if (strncmp(magic, GIF_MAGIC, sizeof(GIF_MAGIC) - 1) == 0)
|
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_GIT87;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
else if (strncmp(magic, GIF89_MAGIC, sizeof(GIF89_MAGIC) - 1) == 0)
|
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_GIT89;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
char *ptr1;
|
|
|
|
char *ptr2;
|
2016-11-10 19:21:47 +01:00
|
|
|
|
2016-11-09 18:14:35 +01:00
|
|
|
/* MS-DOS PCX files will have no magic number, we'll have to make an
|
|
|
|
* educated guess based on the file extension.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Search for the PCX extension */
|
|
|
|
|
|
|
|
for (ptr1 = filename; ((*ptr1) && (*ptr1 != '.')); ptr1++);
|
|
|
|
|
|
|
|
/* Check if the extension matches */
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
for (ptr2 = (char *)g_pcx_extension; ((*ptr1) && (*ptr2)); ptr1++, ptr2++)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 18:42:37 +01:00
|
|
|
if (toupper((int)*ptr1) != *ptr2)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_UNKNOWN;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
/* If it is an exact match, both pointers should refer to the NULL
|
|
|
|
* terminator.
|
2016-11-09 18:14:35 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (!(*ptr1) && !(*ptr2))
|
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_PCX;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
2016-11-10 19:21:47 +01:00
|
|
|
}
|
2016-11-09 18:14:35 +01:00
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
return FORMAT_UNKNOWN;
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
2018-08-13 23:52:59 +02:00
|
|
|
* Name: wld_readgraphic_file
|
|
|
|
*
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
2018-08-13 23:52:59 +02:00
|
|
|
*
|
2016-11-09 18:14:35 +01:00
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 18:42:37 +01:00
|
|
|
graphic_file_t *wld_readgraphic_file(char *filename)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
FILE *fp;
|
2016-11-10 18:42:37 +01:00
|
|
|
graphic_file_format_t format;
|
|
|
|
graphic_file_t *gfile = NULL;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
if ((fp = fopen(filename, "rb")) == NULL)
|
|
|
|
{
|
2016-11-09 18:54:33 +01:00
|
|
|
wld_fatal_error("Could not open texture %s", filename);
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
format = wld_check_format(fp, filename);
|
2016-11-09 18:14:35 +01:00
|
|
|
fseek(fp, 0, SEEK_SET);
|
|
|
|
|
|
|
|
switch (format)
|
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
case FORMAT_GIT89:
|
|
|
|
case FORMAT_GIT87:
|
2016-11-09 18:14:35 +01:00
|
|
|
gfile = wld_LoadGIF(fp, filename);
|
|
|
|
break;
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
case FORMAT_PPM:
|
2016-11-09 18:14:35 +01:00
|
|
|
gfile = wld_LoadPPM(fp, filename);
|
|
|
|
break;
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
case FORMAT_PCX:
|
2016-11-09 19:29:30 +01:00
|
|
|
gfile = wld_loadpcx(fp, filename);
|
2016-11-09 18:14:35 +01:00
|
|
|
break;
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
case FORMAT_UNKNOWN:
|
2016-11-09 18:54:33 +01:00
|
|
|
wld_fatal_error("Unknown graphic file format.\n");
|
2016-11-09 18:14:35 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2016-11-09 18:54:33 +01:00
|
|
|
wld_fatal_error("The graphic file reading code is really broken.\n");
|
2016-11-09 18:14:35 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gfile == NULL)
|
|
|
|
{
|
2016-11-11 21:17:05 +01:00
|
|
|
wld_fatal_error("ERROR: Reading texture %s\n");
|
2016-11-09 18:14:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return gfile;
|
|
|
|
}
|