51 XWDColor xwdcolors [256];
54 unsigned char *imagedata;
61 int w, h, depth, ncolors;
64 Colormap stdcmap =
Scr->RootColormaps.cwins[0]->colormap->c;
69 unsigned long swaptest = 1;
72 if(filename [0] ==
'|') {
73 file = (FILE *) popen(filename + 1,
"r");
87 file = fopen(fullname,
"r");
91 fprintf(stderr,
"unable to locate %s\n", filename);
96 len = fread((
char *) &header,
sizeof(header), 1, file);
98 fprintf(stderr,
"ctwm: cannot read %s\n", filename);
101 if(*(
char *) &swaptest) {
102 swaplong((
char *) &header,
sizeof(header));
104 if(header.file_version != XWD_FILE_VERSION) {
105 fprintf(stderr,
"ctwm: XWD file format version mismatch : %s\n", filename);
108 win_name_size = header.header_size -
sizeof(header);
109 len = fread(win_name, win_name_size, 1, file);
111 fprintf(stderr,
"file %s has not the correct format\n", filename);
115 if(header.pixmap_format == XYPixmap) {
116 fprintf(stderr,
"ctwm: XYPixmap XWD file not supported : %s\n", filename);
119 w = header.pixmap_width;
120 h = header.pixmap_height;
121 depth = header.pixmap_depth;
122 ncolors = header.ncolors;
123 len = fread((
char *) xwdcolors,
sizeof(XWDColor), ncolors, file);
125 fprintf(stderr,
"file %s has not the correct format\n", filename);
128 if(*(
char *) &swaptest) {
129 for(i = 0; i < ncolors; i++) {
130 swaplong((
char *) &xwdcolors [i].pixel, 4);
131 swapshort((
char *) &xwdcolors [i].red, 3 * 2);
134 for(i = 0; i < ncolors; i++) {
135 colors [i].pixel = xwdcolors [i].pixel;
136 colors [i].red = xwdcolors [i].red;
137 colors [i].green = xwdcolors [i].green;
138 colors [i].blue = xwdcolors [i].blue;
139 colors [i].flags = xwdcolors [i].flags;
140 colors [i].pad = xwdcolors [i].pad;
145 visual =
Scr->d_visual;
146 gc = DefaultGC(
dpy, scrn);
148 buffer_size = header.bytes_per_line * h;
149 imagedata = malloc(buffer_size);
151 fprintf(stderr,
"cannot allocate memory for image %s\n", filename);
154 len = fread(imagedata, (
int) buffer_size, 1, file);
157 fprintf(stderr,
"file %s has not the correct format\n", filename);
167 image = XCreateImage(
dpy, visual, depth, header.pixmap_format,
168 0, (
char *) imagedata, w, h,
169 header.bitmap_pad, header.bytes_per_line);
172 fprintf(stderr,
"cannot create image for %s\n", filename);
175 if(header.pixmap_format == ZPixmap) {
178 if(header.pixmap_format != XYBitmap) {
179 for(i = 0; i < ncolors; i++) {
180 XAllocColor(
dpy, cmap, &(colors [i]));
182 for(i = 0; i < buffer_size; i++) {
183 imagedata [i] = (
unsigned char) colors [imagedata [i]].pixel;
195 fprintf(stderr,
"unable to allocate memory for image : %s\n", filename);
198 for(i = 0; i < ncolors; i++) {
199 XFreeColors(
dpy, cmap, &(colors [i].pixel), 1, 0L);
203 if(header.pixmap_format == XYBitmap) {
204 gcvalues.foreground = cp.
fore;
205 gcvalues.background = cp.
back;
206 XChangeGC(
dpy, gc, GCForeground | GCBackground, &gcvalues);
208 if((w > (
Scr->rootw / 2)) || (h > (
Scr->rooth / 2))) {
211 pixret = XCreatePixmap(
dpy,
Scr->Root,
Scr->rootw,
212 Scr->rooth,
Scr->d_depth);
213 x = (
Scr->rootw - w) / 2;
214 y = (
Scr->rooth - h) / 2;
215 XFillRectangle(
dpy, pixret, gc, 0, 0,
Scr->rootw,
Scr->rooth);
216 XPutImage(
dpy, pixret, gc, image, 0, 0,
x,
y, w, h);
221 pixret = XCreatePixmap(
dpy,
Scr->Root, w, h, depth);
222 XPutImage(
dpy, pixret, gc, image, 0, 0, 0, 0, w, h);
226 XDestroyImage(image);
239compress(XImage *image, XColor *colors,
int *ncolors)
241 unsigned char ind [256];
242 unsigned int used [256];
243 int i, j, size, nused;
245 XColor newcolors [256];
246 unsigned char *imagedata;
248 for(i = 0; i < 256; i++) {
253 size = image->bytes_per_line * image->
height;
254 imagedata = (
unsigned char *) image->data;
255 for(i = 0; i < size; i++) {
256 if((i % image->bytes_per_line) > image->width) {
259 color = imagedata [i];
260 if(used [color] == 0) {
261 for(j = 0; j < nused; j++) {
262 if((colors [color].red == newcolors [j].red) &&
263 (colors [color].green == newcolors [j].green) &&
264 (colors [color].blue == newcolors [j].blue)) {
271 newcolors [j].red = colors [color].red;
272 newcolors [j].green = colors [color].green;
273 newcolors [j].blue = colors [color].blue;
278 for(i = 0; i < size; i++) {
279 imagedata [i] = ind [imagedata [i]];
281 for(i = 0; i < nused; i++) {
282 colors [i] = newcolors [i];