2016-11-09 18:14:35 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* apps/graphics/traveler/tools/libwld/wld_loadpcx.c
|
2016-11-10 20:48:43 +01:00
|
|
|
* This file contains the logic which loads a PCX file.
|
2016-11-09 18:14:35 +01:00
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include "trv_types.h"
|
|
|
|
#include "wld_mem.h"
|
|
|
|
#if (!MSWINDOWS)
|
2016-11-10 19:21:47 +01:00
|
|
|
# include "wld_bitmaps.h"
|
|
|
|
# include "wld_graphicfile.h"
|
2016-11-09 18:14:35 +01:00
|
|
|
#endif
|
|
|
|
#include "wld_pcx.h"
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
*************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static void wld_load_pcxheader(FILE * fp, pcx_header_t * header);
|
|
|
|
static void wld_load_pcxdata(FILE * fp, int32_t imagSize,
|
|
|
|
uint8_t * imageBuffer);
|
|
|
|
static void wld_load_pcxpalette(FILE * fp, color_rgb_t * palette);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 20:48:43 +01:00
|
|
|
* Private Functions
|
2016-11-09 18:14:35 +01:00
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 23:20:35 +01:00
|
|
|
* Name: wld_load_pcxheader
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static void wld_load_pcxheader(FILE * fp, pcx_header_t * header)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
uint8_t *tempBuffer;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Load the header */
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
tempBuffer = (uint8_t *) header;
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
for (i = 0; i < SIZEOF_PCX_HEADER; i++)
|
|
|
|
{
|
|
|
|
tempBuffer[i] = getc(fp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 23:20:35 +01:00
|
|
|
* Name: wld_load_pcxdata
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static void wld_load_pcxdata(FILE * fp, int32_t imageSize, uint8_t * imageBuffer)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
2016-11-10 13:29:25 +01:00
|
|
|
uint32_t count;
|
2016-11-10 19:21:47 +01:00
|
|
|
int16_t numBytes;
|
2016-11-09 18:14:35 +01:00
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
count = 0;
|
2016-11-10 19:21:47 +01:00
|
|
|
while (count <= imageSize)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
/* Get the first piece of data */
|
|
|
|
|
|
|
|
data = getc(fp);
|
|
|
|
|
|
|
|
/* Is this a run length encoding? */
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
if ((data >= 192) /* && (data <= 255) */ )
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
/* How many bytes in run? */
|
|
|
|
|
|
|
|
numBytes = data - 192;
|
|
|
|
|
|
|
|
/* Get the actual data for the run */
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
data = getc(fp);
|
2016-11-09 18:14:35 +01:00
|
|
|
|
|
|
|
/* Replicate data in buffer numBytes times */
|
|
|
|
|
2016-11-10 19:21:47 +01:00
|
|
|
while (numBytes-- > 0)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
imageBuffer[count++] = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Actual data, just copy it into buffer at next location */
|
|
|
|
|
|
|
|
imageBuffer[count++] = data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
2016-11-10 23:20:35 +01:00
|
|
|
* Name: wld_load_pcxpalette
|
2016-11-09 18:14:35 +01:00
|
|
|
* Description:
|
|
|
|
************************************************************************/
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
static void wld_load_pcxpalette(FILE * fp, color_rgb_t * palette)
|
2016-11-09 18:14:35 +01:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Move to end of file then back up 768 bytes i.e. to begining of palette */
|
|
|
|
|
|
|
|
fseek(fp, -768L, SEEK_END);
|
|
|
|
|
|
|
|
/* Load the PCX palette into the palette structure */
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++)
|
|
|
|
{
|
|
|
|
/* Get the RGB components */
|
|
|
|
|
|
|
|
#if MSWINDOWS
|
2016-11-10 19:21:47 +01:00
|
|
|
palette[i].red = (getc(fp) >> 2);
|
2016-11-09 18:14:35 +01:00
|
|
|
palette[i].green = (getc(fp) >> 2);
|
2016-11-10 19:21:47 +01:00
|
|
|
palette[i].blue = (getc(fp) >> 2);
|
2016-11-09 18:14:35 +01:00
|
|
|
#else
|
2016-11-10 19:21:47 +01:00
|
|
|
palette[i].red = getc(fp);
|
2016-11-09 18:14:35 +01:00
|
|
|
palette[i].green = getc(fp);
|
2016-11-10 19:21:47 +01:00
|
|
|
palette[i].blue = getc(fp);
|
2016-11-09 18:14:35 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
2016-11-10 20:48:43 +01:00
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Name: wld_loadpcx
|
|
|
|
* Description:
|
|
|
|
* This function loads a pcx file into a picture structure, the actual image
|
|
|
|
* data for the pcx file is decompressed and expanded into a secondary buffer
|
|
|
|
* within the picture structure, the separate images can be grabbed from this
|
|
|
|
* buffer later. also the header and palette are loaded
|
|
|
|
************************************************************************/
|
|
|
|
|
|
|
|
#if MSWINDOWS
|
|
|
|
uint8_t wld_loadpcx(char *filename, pcxPicturePtr image)
|
|
|
|
{
|
|
|
|
FILE *fp, *fopen();
|
|
|
|
uint16_t imageWidth, imageHeight;
|
|
|
|
uint32_t imageSize;
|
|
|
|
|
|
|
|
/* Open the file */
|
|
|
|
|
|
|
|
fp = fopen(filename, "rb");
|
|
|
|
if (!fp)
|
|
|
|
{
|
|
|
|
return PCX_OPEN_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the PCX Header */
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxheader(fp, &image->header);
|
2016-11-10 20:48:43 +01:00
|
|
|
|
|
|
|
/* Load the PCX data */
|
|
|
|
|
|
|
|
if (image->buffer)
|
|
|
|
{
|
|
|
|
imageWidth = image->header.width - image->header.x + 1;
|
|
|
|
imageHeight = image->header.height - image->header.y + 1;
|
|
|
|
imageSize = imageHeight * imageWidth;
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxdata(fp, imageSize, image->buffer);
|
2016-11-10 20:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Load the PCX palette */
|
|
|
|
|
|
|
|
if (image->palette)
|
|
|
|
{
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxpalette(fp, &image->palette);
|
2016-11-10 20:48:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
return PCX_SUCCESS;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
graphic_file_t *wld_loadpcx(FILE * fp, char *filename)
|
|
|
|
{
|
|
|
|
pcx_header_t header;
|
|
|
|
trv_pixel_t *buffer;
|
|
|
|
graphic_file_t *gFile;
|
|
|
|
color_rgb_t *palette;
|
|
|
|
uint16_t imageWidth, imageHeight;
|
|
|
|
uint32_t imageSize;
|
|
|
|
|
|
|
|
/* Load the PCX Header */
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxheader(fp, &header);
|
2016-11-10 20:48:43 +01:00
|
|
|
|
|
|
|
/* Allocate Space to hold the image data */
|
|
|
|
|
|
|
|
imageWidth = header.width - header.x + 1;
|
|
|
|
imageHeight = header.height - header.y + 1;
|
|
|
|
imageSize = imageHeight * imageWidth * sizeof(trv_pixel_t);
|
|
|
|
buffer = (trv_pixel_t *) wld_malloc(imageSize + 1);
|
|
|
|
|
|
|
|
/* Load the PCX data into the buffer */
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxdata(fp, imageSize, (uint8_t *) buffer);
|
2016-11-10 20:48:43 +01:00
|
|
|
|
|
|
|
/* Allocate space to hold the PCX palette */
|
|
|
|
|
|
|
|
palette = (color_rgb_t *) wld_malloc(sizeof(color_rgb_t) * 256);
|
|
|
|
|
|
|
|
/* Load the PCX palette */
|
|
|
|
|
2016-11-10 23:20:35 +01:00
|
|
|
wld_load_pcxpalette(fp, palette);
|
2016-11-10 20:48:43 +01:00
|
|
|
|
|
|
|
/* Now save the resulting data in a graphic_file_t structure */
|
|
|
|
|
|
|
|
gFile = wld_new_graphicfile();
|
2016-11-10 23:20:35 +01:00
|
|
|
gFile->type = GFILE_PALETTED;
|
2016-11-10 20:48:43 +01:00
|
|
|
gFile->palette = palette;
|
|
|
|
gFile->width = imageWidth;
|
|
|
|
gFile->height = imageHeight;
|
|
|
|
gFile->bitmap = buffer;
|
|
|
|
|
|
|
|
return gFile;
|
|
|
|
}
|
|
|
|
#endif
|