360cd0e327634b8f229aef301835cdbe527aa5ec
[rocksndiamonds.git] / src / gifload.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  gifload.c                                               *
12 ***********************************************************/
13
14 #ifndef MSDOS
15 #include "gifload.h"
16
17 #include "image.h"
18
19 #ifdef DEBUG
20 /*
21 #define DEBUG_TIMING
22 */
23 #endif
24
25
26
27 extern long Counter(void);
28
29
30 int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename,
31                         Pixmap *pixmap, Pixmap *pixmap_mask)
32 {
33   Image *image, *image_mask;
34   XImageInfo *ximageinfo, *ximageinfo_mask;
35   int screen;
36   Visual *visual;
37   unsigned int depth;
38
39 #ifdef DEBUG_TIMING
40   long count1, count2;
41   count1 = Counter();
42 #endif
43
44   /* load GIF file */
45   if (!(image = Read_PCX_to_Image(filename)))
46   {
47     printf("Loading GIF image failed -- maybe no GIF...\n");
48     exit(1);
49   }
50
51 #ifdef DEBUG_TIMING
52   count2 = Counter();
53   printf("   LOADING '%s' IN %.2f SECONDS\n",
54          filename, (float)(count2-count1)/1000.0);
55   count1 = Counter();
56 #endif
57
58   if (image->depth > 8)
59   {
60     printf("Sorry, GIFs with more than 256 colors are not supported.\n");
61     exit(1);
62   }
63
64   /* minimize colormap */
65   compress(image);
66
67 #ifdef DEBUG_TIMING
68   count2 = Counter();
69   printf("   COMPRESSING IMAGE COLORMAP IN %.2f SECONDS\n",
70          (float)(count2-count1)/1000.0);
71   count1 = Counter();
72 #endif
73
74   screen = DefaultScreen(display);
75   visual = DefaultVisual(display, screen);
76   depth = DefaultDepth(display, screen);
77
78   /* convert internal image structure to X11 XImage */
79   if (!(ximageinfo = Image_to_XImage(display, screen, visual, depth, image)))
80   {
81     fprintf(stderr, "Cannot convert Image to XImage.\n");
82     exit(1);
83   }
84
85 #ifdef DEBUG_TIMING
86   count2 = Counter();
87   printf("   CONVERTING IMAGE TO XIMAGE IN %.2f SECONDS\n",
88          (float)(count2-count1)/1000.0);
89   count1 = Counter();
90 #endif
91
92   if (ximageinfo->cmap != DefaultColormap(display, screen))
93     XSetWindowColormap(display, window, ximageinfo->cmap);
94
95   /* convert XImage to Pixmap */
96   if ((*pixmap = XImage_to_Pixmap(display, window, ximageinfo)) == None)
97   {
98     fprintf(stderr, "Cannot convert XImage to Pixmap.\n");
99     exit(1);
100   }
101
102 #ifdef DEBUG_TIMING
103   count2 = Counter();
104   printf("   CONVERTING IMAGE TO PIXMAP IN %.2f SECONDS\n",
105          (float)(count2-count1)/1000.0);
106   count1 = Counter();
107 #endif
108
109   /* create mono image for masking */
110   image_mask = monochrome(image);
111
112 #ifdef DEBUG_TIMING
113   count2 = Counter();
114   printf("   CONVERTING IMAGE TO MASK IN %.2f SECONDS\n",
115          (float)(count2-count1)/1000.0);
116   count1 = Counter();
117 #endif
118
119   /* convert internal image structure to X11 XImage */
120   if (!(ximageinfo_mask = Image_to_XImage(display, screen, visual, depth,
121                                         image_mask)))
122   {
123     fprintf(stderr, "Cannot convert Image to XImage.\n");
124     exit(1);
125   }
126
127 #ifdef DEBUG_TIMING
128   count2 = Counter();
129   printf("   CONVERTING MASK TO XIMAGE IN %.2f SECONDS\n",
130          (float)(count2-count1)/1000.0);
131   count1 = Counter();
132 #endif
133
134   /* convert XImage to Pixmap */
135   if ((*pixmap_mask = XImage_to_Pixmap(display, window, ximageinfo_mask)) == None)
136   {
137     fprintf(stderr, "Cannot convert XImage to Pixmap.\n");
138     exit(1);
139   }
140
141 #ifdef DEBUG_TIMING
142   count2 = Counter();
143   printf("   CONVERTING MASK TO PIXMAP IN %.2f SECONDS\n",
144          (float)(count2-count1)/1000.0);
145   count1 = Counter();
146 #endif
147
148   return(GIF_Success);
149 }
150
151 #endif