1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
|
/* SCCS Id: @(#)xpm2iff.c 3.2 95/08/04 */
/* Copyright (c) 1995 by Gregg Wonderly, Naperville, Illinois */
/* NetHack may be freely redistributed. See license for details. */
#include <stdlib.h>
#include "config.h"
#include "tile.h"
#include <dos/dos.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <graphics/gfx.h>
#include <graphics/gfxbase.h>
#include <graphics/view.h>
#include <libraries/iffparse.h>
#include <libraries/dos.h>
#ifndef _DCC
# include <proto/iffparse.h>
# include <proto/dos.h>
# include <proto/exec.h>
#endif
struct xpmscreen {
int Width;
int Height;
int Colors;
int ColorResolution;
int Background;
int AspectRatio;
int Interlace;
int BytesPerRow;
} XpmScreen;
/* translation table from xpm characters to RGB and colormap slots */
struct Ttable {
char flag;
char r,g,b;
int slot; /* output colortable index */
}ttable[256];
pixval ColorMap[3][MAXCOLORMAPSIZE];
int colorsinmap;
/*
* We are using a hybrid form of our own design which we call a BMAP (for
* bitmap) form. It is an ILBM with the bitmaps already deinterleaved,
* completely uncompressed.
* This speeds the loading of the images from the games point of view because it
* does not have to deinterleave and uncompress them.
*/
#define ID_BMAP MAKE_ID( 'B', 'M', 'A', 'P' ) /* instead of ILBM */
#define ID_BMHD MAKE_ID( 'B', 'M', 'H', 'D' ) /* Same as ILBM */
#define ID_CAMG MAKE_ID( 'C', 'A', 'M', 'G' ) /* Same as ILBM */
#define ID_CMAP MAKE_ID( 'C', 'M', 'A', 'P' ) /* Same as ILBM */
#define ID_PDAT MAKE_ID( 'P', 'D', 'A', 'T' ) /* Extra data describing plane
* size due to graphics.library
* rounding requirements.
*/
#define ID_PLNE MAKE_ID( 'P', 'L', 'N', 'E' ) /* The planes of the image */
int nplanes;
/* BMHD from IFF documentation */
typedef struct {
UWORD w, h;
WORD x, y;
UBYTE nPlanes;
UBYTE masking;
UBYTE compression;
UBYTE reserved1;
UWORD transparentColor;
UBYTE xAspect, yAspect;
WORD pageWidth, pageHeight;
} BitMapHeader;
typedef struct {
UBYTE r, g, b;
} AmiColorMap;
pixel pixels[TILE_Y][TILE_X];
AmiColorMap *cmap;
void
error( char *str )
{
fprintf( stderr, "ERROR: %s\n", str );
}
char **planes;
main( int argc, char **argv )
{
int colors;
struct {
long nplanes;
long pbytes;
long across;
long down;
long npics;
long xsize;
long ysize;
} pdat;
long pbytes; /* Bytes of data in a plane */
int i, cnt;
BitMapHeader bmhd;
struct IFFHandle *iff;
long camg = HIRES|LACE;
int tiles=0;
int index;
#if defined(_DCC) || defined (__GNUC__)
IFFParseBase = OpenLibrary( "iffparse.library", 0 );
if( !IFFParseBase ) {
error( "unable to open iffparse.library" );
exit( 1 );
}
#endif
if( fopen_xpm_file( argv[1], "r" ) != TRUE )
{
perror( argv[1] );
return( 1 );
}
nplanes = 0;
i = XpmScreen.Colors - 1;
while( i != 0 )
{
nplanes++;
i >>= 1;
}
planes = malloc( nplanes * sizeof( char * ) );
if( planes == 0 )
{
error( "can not allocate planes pointer" );
exit( 1 );
}
XpmScreen.BytesPerRow = ((XpmScreen.Width + 15)/16)*2;
pbytes = XpmScreen.BytesPerRow * XpmScreen.Height;
for( i = 0; i < nplanes; ++i )
{
planes[ i ] = malloc( pbytes );
if( planes[ i ] == 0 )
{
error( "can not allocate planes pointer" );
exit( 1 );
}
memset( planes[i], 0, pbytes );
}
iff = AllocIFF();
if( !iff )
{
error( "Can not allocate IFFHandle" );
return( 1 );
}
iff->iff_Stream = Open( argv[2], MODE_NEWFILE );
if( !iff->iff_Stream )
{
error( "Can not open output file" );
return( 1 );
}
InitIFFasDOS( iff );
OpenIFF( iff, IFFF_WRITE );
PushChunk( iff, ID_BMAP, ID_FORM, IFFSIZE_UNKNOWN );
bmhd.w = XpmScreen.Width;
bmhd.h = XpmScreen.Height;
bmhd.x = 0;
bmhd.y = 0;
bmhd.nPlanes = nplanes;
bmhd.masking = 0;
bmhd.compression = 0;
bmhd.reserved1 = 0;
bmhd.transparentColor = 0;
bmhd.xAspect = 100;
bmhd.yAspect = 100;
bmhd.pageWidth = 0; /* not needed for this program */
bmhd.pageHeight = 0; /* not needed for this program */
PushChunk( iff, ID_BMAP, ID_BMHD, sizeof( bmhd ) );
WriteChunkBytes( iff, &bmhd, sizeof( bmhd ) );
PopChunk( iff );
PushChunk( iff, ID_BMAP, ID_CAMG, sizeof( camg ) );
WriteChunkBytes( iff, &camg, sizeof( camg ) );
PopChunk( iff );
#define SCALE(x) (x)
cmap = malloc( (colors = (1L<<nplanes)) * sizeof(AmiColorMap) );
if(cmap == 0){
error("Can't allocate color map");
exit(1);
}
for(index = 0; index<256; index++){
if(ttable[index].flag){
cmap[ttable[index].slot].r = SCALE(ttable[index].r);
cmap[ttable[index].slot].g = SCALE(ttable[index].g);
cmap[ttable[index].slot].b = SCALE(ttable[index].b);
}
}
#undef SCALE
PushChunk( iff, ID_BMAP, ID_CMAP, IFFSIZE_UNKNOWN );
WriteChunkBytes( iff, cmap, colors*sizeof(*cmap) );
PopChunk( iff );
conv_image();
pdat.nplanes = nplanes;
pdat.pbytes = pbytes;
pdat.xsize = XpmScreen.Width;
pdat.ysize = XpmScreen.Height;
pdat.across = 0;
pdat.down = 0;
pdat.npics = 1;
PushChunk( iff, ID_BMAP, ID_PDAT, IFFSIZE_UNKNOWN );
WriteChunkBytes( iff, &pdat, sizeof( pdat ) );
PopChunk( iff );
PushChunk( iff, ID_BMAP, ID_PLNE, IFFSIZE_UNKNOWN );
for( i = 0; i < nplanes; ++i )
WriteChunkBytes( iff, planes[i], pbytes );
PopChunk( iff );
CloseIFF( iff );
Close( iff->iff_Stream );
FreeIFF( iff );
#if defined(_DCC) || defined (__GNUC__)
CloseLibrary( IFFParseBase );
#endif
exit( 0 );
}
#define SETBIT(Plane, Plane_offset, Col, Value) \
if(Value){ \
planes[Plane][Plane_offset + (Col/8)] |= 1<<(7-(Col & 7)); \
}
conv_image(){
int row, col, planeno;
for(row = 0;row<XpmScreen.Height;row++){
char *xb = xpmgetline();
int plane_offset;
if(xb==0)return;
plane_offset = row*XpmScreen.BytesPerRow;
for(col = 0;col<XpmScreen.Width;col++){
int slot;
int color = xb[col];
if(!ttable[color].flag){
fprintf(stderr, "Bad image data\n");
}
slot = ttable[color].slot;
for(planeno = 0; planeno<nplanes; planeno++){
SETBIT(planeno, plane_offset, col, slot & (1<<planeno));
}
}
}
}
long *
alloc( unsigned int n )
{
long *ret = malloc( n );
if(!ret){
error("Can't allocate memory");
exit(1);
}
return( ret );
}
FILE *xpmfh = 0;
char initbuf[200];
char *xpmbuf = initbuf;
/* version 1. Reads the raw xpm file, NOT the compiled version. This is
* not a particularly good idea but I don't have time to do the right thing
* at this point, even if I was absolutely sure what that was. */
fopen_xpm_file(const char *fn, const char *mode){
int temp;
char *xb;
if(strcmp(mode, "r"))return FALSE; /* no choice now */
if(xpmfh)return FALSE; /* one file at a time */
xpmfh = fopen(fn, mode);
if(!xpmfh)return FALSE; /* I'm hard to please */
/* read the header */
xb = xpmgetline();
if(xb == 0)return FALSE;
if(4 != sscanf(xb,"%d %d %d %d",
&XpmScreen.Width, &XpmScreen.Height,
&XpmScreen.Colors, &temp))return FALSE; /* bad header */
/* replace the original buffer with one big enough for
* the real data
*/
/* XXX */
xpmbuf = malloc(XpmScreen.Width * 2);
if(!xpmbuf){
error("Can't allocate line buffer");
exit(1);
}
if(temp != 1)return FALSE; /* limitation of this code */
{
/* read the colormap and translation table */
int ccount = -1;
while(ccount++ < (XpmScreen.Colors-1)){
char index;
int r, g, b;
xb = xpmgetline();
if(xb==0)return FALSE;
if(4 != sscanf(xb,"%c c #%2x%2x%2x",&index,&r,&g,&b)){
fprintf(stderr,"Bad color entry: %s\n",xb);
return FALSE;
}
ttable[index].flag = 1; /* this color is valid */
ttable[index].r = r;
ttable[index].g = g;
ttable[index].b = b;
ttable[index].slot = ccount;
}
}
return TRUE;
}
/* This deserves better. Don't read it too closely - you'll get ill. */
#define bufsz 2048
char buf[bufsz];
xpmgetline(){
char *bp;
do {
if(fgets(buf, bufsz, xpmfh) == 0)return 0;
} while(buf[0] != '"');
/* strip off the trailing <",> if any */
for(bp = buf;*bp;bp++);
bp--;
while(isspace(*bp))bp--;
if(*bp==',')bp--;
if(*bp=='"')bp--;
bp++;
*bp = '\0';
return &buf[1];
}
|