326 lines
9.6 KiB
C
326 lines
9.6 KiB
C
/*
|
|
* CDE - Common Desktop Environment
|
|
*
|
|
* Copyright (c) 1993-2012, The Open Group. All rights reserved.
|
|
*
|
|
* These libraries and programs are free software; you can
|
|
* redistribute them and/or modify them under the terms of the GNU
|
|
* Lesser General Public License as published by the Free Software
|
|
* Foundation; either version 2 of the License, or (at your option)
|
|
* any later version.
|
|
*
|
|
* These libraries and programs are distributed in the hope that
|
|
* they will be useful, but WITHOUT ANY WARRANTY; without even the
|
|
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU Lesser General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with these librararies and programs; if not, write
|
|
* to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
|
|
* Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
/* $XConsortium: XbmUtils.c /main/4 1996/05/09 03:44:48 drk $ */
|
|
/*
|
|
** XbmUtils.c
|
|
**
|
|
** This module provides routines that allow a pixmap to be generated from
|
|
** a _DtGrStream containing XBM data. The module is adapted from the
|
|
** RdBitF.c module in the X11R6 version of libX11.
|
|
*/
|
|
|
|
/*
|
|
|
|
Copyright (c) 1987 X Consortium
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
a copy of this software and associated documentation files (the
|
|
"Software"), to deal in the Software without restriction, including
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
in all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
|
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
Except as contained in this notice, the name of the X Consortium shall
|
|
not be used in advertising or otherwise to promote the sale, use or
|
|
other dealings in this Software without prior written authorization
|
|
from the X Consortium.
|
|
|
|
*/
|
|
|
|
/*
|
|
* Code to read bitmaps from disk files. Interprets
|
|
* data from X10 and X11 bitmap files and creates
|
|
* Pixmap representations of files. Returns Pixmap
|
|
* ID and specifics about image.
|
|
*
|
|
* Modified for speedup by Jim Becker, changed image
|
|
* data parsing logic (removed some fscanf()s).
|
|
* Aug 5, 1988
|
|
*
|
|
* Note that this file and ../Xmu/RdBitF.c look very similar.... Keep them
|
|
* that way (but don't use common source code so that people can have one
|
|
* without the other).
|
|
*/
|
|
|
|
#include <X11/IntrinsicP.h>
|
|
#include <X11/Xlibint.h>
|
|
#include <X11/Xos.h>
|
|
#include <X11/Xutil.h>
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
|
|
#include "GraphicsP.h"
|
|
#include "Lock.h"
|
|
|
|
#define MAX_SIZE 255
|
|
|
|
/* shared data for the image read/parse logic */
|
|
static short hexTable[256]; /* conversion value */
|
|
|
|
/*
|
|
* Table index for the hex values. Initialized once, first time.
|
|
* Used for translation value or delimiter significance lookup.
|
|
*/
|
|
static void initHexTable()
|
|
{
|
|
/*
|
|
* We build the table at run time for several reasons:
|
|
*
|
|
* 1. portable to non-ASCII machines.
|
|
* 2. still reentrant since we set the init flag after setting table.
|
|
* 3. easier to extend.
|
|
* 4. less prone to bugs.
|
|
*/
|
|
hexTable['0'] = 0; hexTable['1'] = 1;
|
|
hexTable['2'] = 2; hexTable['3'] = 3;
|
|
hexTable['4'] = 4; hexTable['5'] = 5;
|
|
hexTable['6'] = 6; hexTable['7'] = 7;
|
|
hexTable['8'] = 8; hexTable['9'] = 9;
|
|
hexTable['A'] = 10; hexTable['B'] = 11;
|
|
hexTable['C'] = 12; hexTable['D'] = 13;
|
|
hexTable['E'] = 14; hexTable['F'] = 15;
|
|
hexTable['a'] = 10; hexTable['b'] = 11;
|
|
hexTable['c'] = 12; hexTable['d'] = 13;
|
|
hexTable['e'] = 14; hexTable['f'] = 15;
|
|
|
|
/* delimiters of significance are flagged w/ negative value */
|
|
hexTable[' '] = -1; hexTable[','] = -1;
|
|
hexTable['}'] = -1; hexTable['\n'] = -1;
|
|
hexTable['\t'] = -1;
|
|
}
|
|
|
|
/*
|
|
* read next hex value in the input stream, return -1 if EOF
|
|
*/
|
|
static int NextInt (fstream)
|
|
_DtGrStream *fstream;
|
|
{
|
|
int ch;
|
|
int value = 0;
|
|
int gotone = 0;
|
|
int done = 0;
|
|
|
|
/* loop, accumulate hex value until find delimiter */
|
|
/* skip any initial delimiters found in read stream */
|
|
|
|
_DtHelpProcessLock();
|
|
while (!done) {
|
|
ch = _DtGrGetChar(fstream);
|
|
if (ch == EOF) {
|
|
value = -1;
|
|
done++;
|
|
} else {
|
|
/* trim high bits, check type and accumulate */
|
|
ch &= 0xff;
|
|
if (isascii(ch) && isxdigit(ch)) {
|
|
value = (value << 4) + hexTable[ch];
|
|
gotone++;
|
|
} else if ((hexTable[ch]) < 0 && gotone)
|
|
done++;
|
|
}
|
|
}
|
|
_DtHelpProcessUnlock();
|
|
return value;
|
|
}
|
|
|
|
#if NeedFunctionPrototypes
|
|
int _DtGrReadBitmapStreamData (
|
|
_DtGrStream *fstream,
|
|
unsigned int *width, /* RETURNED */
|
|
unsigned int *height, /* RETURNED */
|
|
unsigned char **data, /* RETURNED */
|
|
int *x_hot, /* RETURNED */
|
|
int *y_hot) /* RETURNED */
|
|
#else
|
|
int _DtGrReadBitmapStreamData (stream, width, height, data, x_hot, y_hot)
|
|
_DtGrStream *fstream;
|
|
unsigned int *width, *height; /* RETURNED */
|
|
unsigned char **data; /* RETURNED */
|
|
int *x_hot, *y_hot; /* RETURNED */
|
|
#endif
|
|
{
|
|
unsigned char *bits = NULL; /* working variable */
|
|
char line[MAX_SIZE]; /* input line from file */
|
|
int size; /* number of bytes of data */
|
|
char name_and_type[MAX_SIZE]; /* an input line */
|
|
char *type; /* for parsing */
|
|
int value; /* from an input line */
|
|
int version10p; /* boolean, old format */
|
|
int padding; /* to handle alignment */
|
|
int bytes_per_line; /* per scanline of data */
|
|
unsigned int ww = 0; /* width */
|
|
unsigned int hh = 0; /* height */
|
|
int hx = -1; /* x hotspot */
|
|
int hy = -1; /* y hotspot */
|
|
static Bool initialized = False; /* easier to fill in at run time */
|
|
|
|
/* first time initialization */
|
|
_DtHelpProcessLock();
|
|
if (initialized == False)
|
|
{
|
|
initHexTable();
|
|
initialized = True;
|
|
}
|
|
_DtHelpProcessUnlock();
|
|
|
|
/* error cleanup and return macro */
|
|
#define RETURN(code) \
|
|
{ if (bits) Xfree ((char *)bits); return code; }
|
|
|
|
while (_DtGrGetString(line, MAX_SIZE, fstream)) {
|
|
if (strlen(line) == MAX_SIZE-1)
|
|
RETURN (BitmapFileInvalid);
|
|
if (sscanf(line,"#define %s %d",name_and_type,&value) == 2) {
|
|
if (!(type = strrchr(name_and_type, '_')))
|
|
type = name_and_type;
|
|
else
|
|
type++;
|
|
|
|
if (!strcmp("width", type))
|
|
ww = (unsigned int) value;
|
|
if (!strcmp("height", type))
|
|
hh = (unsigned int) value;
|
|
if (!strcmp("hot", type)) {
|
|
if (type-- == name_and_type || type-- == name_and_type)
|
|
continue;
|
|
if (!strcmp("x_hot", type))
|
|
hx = value;
|
|
if (!strcmp("y_hot", type))
|
|
hy = value;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if (sscanf(line, "static short %s = {", name_and_type) == 1)
|
|
version10p = 1;
|
|
else if (sscanf(line,"static unsigned char %s = {",name_and_type) == 1)
|
|
version10p = 0;
|
|
else if (sscanf(line, "static char %s = {", name_and_type) == 1)
|
|
version10p = 0;
|
|
else
|
|
continue;
|
|
|
|
if (!(type = strrchr(name_and_type, '_')))
|
|
type = name_and_type;
|
|
else
|
|
type++;
|
|
|
|
if (strcmp("bits[]", type))
|
|
continue;
|
|
|
|
if (!ww || !hh)
|
|
RETURN (BitmapFileInvalid);
|
|
|
|
if ((ww % 16) && ((ww % 16) < 9) && version10p)
|
|
padding = 1;
|
|
else
|
|
padding = 0;
|
|
|
|
bytes_per_line = (ww+7)/8 + padding;
|
|
|
|
size = bytes_per_line * hh;
|
|
bits = (unsigned char *) Xmalloc ((unsigned int) size);
|
|
if (!bits)
|
|
RETURN (BitmapNoMemory);
|
|
|
|
if (version10p) {
|
|
unsigned char *ptr;
|
|
int bytes;
|
|
|
|
for (bytes=0, ptr=bits; bytes<size; (bytes += 2)) {
|
|
if ((value = NextInt(fstream)) < 0)
|
|
RETURN (BitmapFileInvalid);
|
|
*(ptr++) = value;
|
|
if (!padding || ((bytes+2) % bytes_per_line))
|
|
*(ptr++) = value >> 8;
|
|
}
|
|
} else {
|
|
unsigned char *ptr;
|
|
int bytes;
|
|
|
|
for (bytes=0, ptr=bits; bytes<size; bytes++, ptr++) {
|
|
if ((value = NextInt(fstream)) < 0)
|
|
RETURN (BitmapFileInvalid);
|
|
*ptr=value;
|
|
}
|
|
}
|
|
} /* end while */
|
|
|
|
if (!bits)
|
|
return (BitmapFileInvalid);
|
|
|
|
*data = bits;
|
|
*width = ww;
|
|
*height = hh;
|
|
if (x_hot) *x_hot = hx;
|
|
if (y_hot) *y_hot = hy;
|
|
|
|
return (BitmapSuccess);
|
|
}
|
|
|
|
#if NeedFunctionPrototypes
|
|
int _DtGrReadBitmapStream (
|
|
Display *display,
|
|
Drawable d,
|
|
_DtGrStream *stream,
|
|
unsigned int *width, /* RETURNED */
|
|
unsigned int *height, /* RETURNED */
|
|
Pixmap *pixmap, /* RETURNED */
|
|
int *x_hot, /* RETURNED */
|
|
int *y_hot) /* RETURNED */
|
|
#else
|
|
int _DtGrReadBitmapStream (display, d, stream, width, height, pixmap, x_hot, y_hot)
|
|
Display *display;
|
|
Drawable d;
|
|
_DtGrStream *stream;
|
|
char *filename;
|
|
unsigned int *width, *height; /* RETURNED */
|
|
Pixmap *pixmap; /* RETURNED */
|
|
int *x_hot, *y_hot; /* RETURNED */
|
|
#endif
|
|
{
|
|
unsigned char *data;
|
|
int res;
|
|
|
|
res = _DtGrReadBitmapStreamData(stream, width, height, &data, x_hot, y_hot);
|
|
if (res != BitmapSuccess)
|
|
return res;
|
|
*pixmap = XCreateBitmapFromData(display, d, (char *)data, *width, *height);
|
|
Xfree((char *)data);
|
|
if (*pixmap == None)
|
|
return (BitmapNoMemory);
|
|
return (BitmapSuccess);
|
|
}
|