File: readWriteXPM.c

package info (click to toggle)
xpaint 2.5.1-2
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,488 kB
  • ctags: 2,478
  • sloc: ansic: 25,980; makefile: 36; sh: 23
file content (148 lines) | stat: -rw-r--r-- 3,827 bytes parent folder | download | duplicates (4)
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
/* +-------------------------------------------------------------------+ */
/* | Copyright 1993, David Koblas (koblas@netcom.com)		       | */
/* |								       | */
/* | Permission to use, copy, modify, and to distribute this software  | */
/* | and its documentation for any purpose is hereby granted without   | */
/* | fee, provided that the above copyright notice appear in all       | */
/* | copies and that both that copyright notice and this permission    | */
/* | notice appear in supporting documentation.	 There is no	       | */
/* | representations about the suitability of this software for	       | */
/* | any purpose.  this software is provided "as is" without express   | */
/* | or implied warranty.					       | */
/* |								       | */
/* +-------------------------------------------------------------------+ */

/* $Id: readWriteXPM.c,v 1.5 1996/11/01 09:38:47 torsten Exp $ */

#include <stdio.h>
#include <X11/Intrinsic.h>
#include <xpm.h>
#include "xpaint.h"
#include "image.h"
#include "rwTable.h"

int WriteXPM(char *file, Image * image)
{
    XpmAttributes attr;

    attr.valuemask = XpmColormap;
    attr.colormap = (Colormap) image->sourceColormap;

    return XpmWriteFileFromPixmap(Global.display, file,
	    image->sourcePixmap, image->sourceMask, &attr) != XpmSuccess;
}

int TestXPM(char *file)
{
    FILE *fd = fopen(file, "r");
    char buf[40];
    int i, n, ret = 0;
    int cstart = False;

    if (fd == NULL)
	return 0;

    n = fread(buf, sizeof(char), sizeof(buf), fd);

    for (i = 0; i < n && ret == 0; i++) {
	if (!cstart) {
	    if (buf[i] == '/' && buf[i + 1] == '*')
		cstart = True;
	} else {
	    if (buf[i] == 'X' && buf[i + 1] == 'P' && buf[i + 2] == 'M')
		ret = 1;
	}
    }

    fclose(fd);
    return ret;
}

Image *
 ReadXPM(char *file)
{
    Display *dpy = Global.display;
    XImage *xim, *mim;
    int x, y, i;
    Image *image;
    XpmAttributes attr;
    XColor *col;
    unsigned char *ucp, *ump;
    unsigned short *usp;
    Colormap cmap = DefaultColormap(dpy, DefaultScreen(dpy));
    int status;

    attr.valuemask = XpmReturnPixels;
    if ((status = XpmReadFileToImage(dpy, file, &xim, &mim, &attr)) != XpmSuccess) {
	switch (status) {
	case XpmColorError:
	    RWSetMsg("XPM Color Error");
	    break;
	case XpmSuccess:
	    RWSetMsg("Success, shouldn't have error & success");
	    break;
	case XpmOpenFailed:
	    RWSetMsg("XPM Open Failed");
	    break;
	case XpmFileInvalid:
	    RWSetMsg("File Invalid");
	    break;
	case XpmNoMemory:
	    RWSetMsg("Not enough memory");
	    break;
	case XpmColorFailed:
	    RWSetMsg("Unable to allocate color");
	    break;
	}
	XpmFreeAttributes(&attr);
	return NULL;
    }
    image = ImageNewCmap(attr.width, attr.height, attr.npixels);
    if (mim != NULL) {
	ImageMakeMask(image);
	ump = image->maskData;
    }
    ucp = (unsigned char *) image->data;
    usp = (unsigned short *) image->data;

    col = (XColor *) XtMalloc(sizeof(XColor) * attr.npixels);
    for (i = 0; i < attr.npixels; i++) {
	col[i].pixel = attr.pixels[i];
	col[i].flags = DoRed | DoGreen | DoBlue;
    }
    XQueryColors(dpy, cmap, col, attr.npixels);
    for (i = 0; i < attr.npixels; i++)
	ImageSetCmap(image, i, col[i].red >> 8, col[i].green >> 8, col[i].blue >> 8);

    for (y = 0; y < xim->height; y++) {
	for (x = 0; x < xim->width; x++) {
	    Pixel p;

	    if (mim != NULL) {
		Pixel f = XGetPixel(mim, x, y);

		if (!(*ump++ = (Boolean) f)) {
		    if (attr.npixels > 256)
			*usp++ = 0;
		    else
			*ucp++ = 0;
		    continue;
		}
	    }
	    p = XGetPixel(xim, x, y);

	    for (i = 0; i < attr.npixels && col[i].pixel != p; i++);

	    if (attr.npixels > 256)
		*usp++ = i;
	    else
		*ucp++ = i;
	}
    }

    XtFree((XtPointer) col);
    XDestroyImage(xim);
    XpmFreeAttributes(&attr);

    return image;
}