27 |
#include <malloc.h> |
#include <malloc.h> |
28 |
#include "pixmap.h" |
#include "pixmap.h" |
29 |
|
|
30 |
int loadPixmap(Display * dpy, MyPixmap * pm, char *dir, char *file, |
int |
31 |
XpmColorSymbol * cs, int n) |
loadPixmap (Display * dpy, MyPixmap * pm, char *dir, char *file, |
32 |
|
XpmColorSymbol * cs, int n) |
33 |
{ |
{ |
34 |
char filename[512]; |
char filename[512]; |
35 |
XpmAttributes attr; |
XpmAttributes attr; |
36 |
|
|
37 |
#ifdef DEBUG |
#ifdef DEBUG |
38 |
printf("entering loadPixmap\n"); |
printf ("entering loadPixmap\n"); |
39 |
#endif |
#endif |
40 |
|
|
41 |
pm->pixmap = None; |
pm->pixmap = None; |
42 |
pm->mask = None; |
pm->mask = None; |
43 |
pm->width = 1; |
pm->width = 1; |
44 |
pm->height = 1; |
pm->height = 1; |
45 |
snprintf(filename, sizeof(filename), "%s/%s", dir, file); |
snprintf (filename, sizeof (filename), "%s/%s", dir, file); |
46 |
attr.colorsymbols = cs; |
attr.colorsymbols = cs; |
47 |
attr.numsymbols = n; |
attr.numsymbols = n; |
48 |
attr.valuemask = XpmSize; |
attr.valuemask = XpmSize; |
49 |
if(n > 0 && cs) |
if (n > 0 && cs) |
50 |
attr.valuemask = attr.valuemask | XpmColorSymbols; |
attr.valuemask = attr.valuemask | XpmColorSymbols; |
51 |
if(XpmReadFileToPixmap |
if (XpmReadFileToPixmap |
52 |
(dpy, XDefaultRootWindow(dpy), filename, &pm->pixmap, &pm->mask, &attr)) |
(dpy, XDefaultRootWindow (dpy), filename, &pm->pixmap, &pm->mask, |
53 |
return False; |
&attr)) |
54 |
pm->width = attr.width; |
return False; |
55 |
pm->height = attr.height; |
pm->width = attr.width; |
56 |
XpmFreeAttributes(&attr); |
pm->height = attr.height; |
57 |
return True; |
XpmFreeAttributes (&attr); |
58 |
|
return True; |
59 |
} |
} |
60 |
|
|
61 |
void freePixmap(Display * dpy, MyPixmap * pm) |
void |
62 |
|
freePixmap (Display * dpy, MyPixmap * pm) |
63 |
{ |
{ |
64 |
#ifdef DEBUG |
#ifdef DEBUG |
65 |
printf("entering freePixmap\n"); |
printf ("entering freePixmap\n"); |
66 |
#endif |
#endif |
67 |
|
|
68 |
if(pm->pixmap != None) |
if (pm->pixmap != None) |
69 |
XFreePixmap(dpy, pm->pixmap); |
XFreePixmap (dpy, pm->pixmap); |
70 |
if(pm->mask != None) |
if (pm->mask != None) |
71 |
XFreePixmap(dpy, pm->mask); |
XFreePixmap (dpy, pm->mask); |
72 |
} |
} |
73 |
|
|
74 |
void scalePixmap(Display * dpy, MyPixmap * src, MyPixmap * dst, int width, |
void |
75 |
int height) |
scalePixmap (Display * dpy, MyPixmap * src, MyPixmap * dst, int width, |
76 |
|
int height) |
77 |
{ |
{ |
78 |
XpmImage xi_src, xi_dst; |
XpmImage xi_src, xi_dst; |
79 |
int x, y, sx, sy, *src_data, *dst_data; |
int x, y, sx, sy, *src_data, *dst_data; |
80 |
|
|
81 |
#ifdef DEBUG |
#ifdef DEBUG |
82 |
printf("entering scalePixmap\n"); |
printf ("entering scalePixmap\n"); |
83 |
#endif |
#endif |
84 |
|
|
85 |
/* don't ask me why but sometimes this function gets _REALLY_ big imagesizes |
/* don't ask me why but sometimes this function gets _REALLY_ big imagesizes |
86 |
* so there must be something totally wrong somewhere... */ |
* so there must be something totally wrong somewhere... */ |
87 |
if(width > 20000) |
if (width > 20000) |
88 |
return; |
return; |
89 |
if(height > 20000) |
if (height > 20000) |
90 |
return; |
return; |
91 |
if(width < 1) |
if (width < 1) |
92 |
return; |
return; |
93 |
if(height < 1) |
if (height < 1) |
94 |
return; |
return; |
95 |
/* I currently don't know exactly but it _IS_ a bug in here */ |
/* I currently don't know exactly but it _IS_ a bug in here */ |
96 |
|
|
97 |
XpmCreateXpmImageFromPixmap(dpy, src->pixmap, src->mask, &xi_src, NULL); |
XpmCreateXpmImageFromPixmap (dpy, src->pixmap, src->mask, &xi_src, NULL); |
98 |
dst->width = width; |
dst->width = width; |
99 |
dst->height = height; |
dst->height = height; |
100 |
xi_dst.width = width; |
xi_dst.width = width; |
101 |
xi_dst.height = height; |
xi_dst.height = height; |
102 |
xi_dst.cpp = xi_src.cpp; |
xi_dst.cpp = xi_src.cpp; |
103 |
xi_dst.ncolors = xi_src.ncolors; |
xi_dst.ncolors = xi_src.ncolors; |
104 |
xi_dst.colorTable = xi_src.colorTable; |
xi_dst.colorTable = xi_src.colorTable; |
105 |
xi_dst.data = malloc(sizeof(int) * (xi_dst.width * xi_dst.height)); |
xi_dst.data = malloc (sizeof (int) * (xi_dst.width * xi_dst.height)); |
106 |
dst_data = xi_dst.data; |
dst_data = xi_dst.data; |
107 |
src_data = xi_src.data; |
src_data = xi_src.data; |
108 |
#ifdef DEBUG |
#ifdef DEBUG |
109 |
printf("xi_dst.width %i\n",xi_dst.width); |
printf ("xi_dst.width %i\n", xi_dst.width); |
110 |
printf("xi_dst.height %i\n",xi_dst.height); |
printf ("xi_dst.height %i\n", xi_dst.height); |
111 |
printf("xi_src.width %i\n",xi_src.width); |
printf ("xi_src.width %i\n", xi_src.width); |
112 |
printf("xi_src.height %i\n",xi_src.height); |
printf ("xi_src.height %i\n", xi_src.height); |
113 |
#endif |
#endif |
114 |
for(y = 0; y < xi_dst.height; y++) |
for (y = 0; y < xi_dst.height; y++) |
115 |
{ |
{ |
116 |
dst_data = xi_dst.data + (y * xi_dst.width); |
dst_data = xi_dst.data + (y * xi_dst.width); |
117 |
for(x = 0; x < xi_dst.width; x++) |
for (x = 0; x < xi_dst.width; x++) |
118 |
{ |
{ |
119 |
sx = (x * xi_src.width) / xi_dst.width; |
sx = (x * xi_src.width) / xi_dst.width; |
120 |
sy = (y * xi_src.height) / xi_dst.height; |
sy = (y * xi_src.height) / xi_dst.height; |
121 |
*dst_data = *(src_data + sx + (sy * xi_src.width)); |
*dst_data = *(src_data + sx + (sy * xi_src.width)); |
122 |
dst_data++; |
dst_data++; |
123 |
} |
} |
124 |
} |
} |
125 |
XpmCreatePixmapFromXpmImage(dpy, DefaultRootWindow(dpy), &xi_dst, |
XpmCreatePixmapFromXpmImage (dpy, DefaultRootWindow (dpy), &xi_dst, |
126 |
&dst->pixmap, &dst->mask, NULL); |
&dst->pixmap, &dst->mask, NULL); |
127 |
free(xi_dst.data); |
free (xi_dst.data); |
128 |
XpmFreeXpmImage(&xi_src); |
XpmFreeXpmImage (&xi_src); |
129 |
} |
} |