rnd-19980930-1
[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 "xli.h"
18
19 #ifdef DEBUG
20 /*
21 #define DEBUG_TIMING
22 */
23 #endif
24
25
26
27 extern long Counter(void);
28
29
30
31 int Read_GIF_to_Pixmaps(Display *display, Window window, char *filename,
32                         Pixmap *pixmap, Pixmap *pixmap_mask)
33 {
34   Image *image, *image_mask;
35   XImageInfo *ximageinfo, *ximageinfo_mask;
36   int screen;
37   Visual *visual;
38   unsigned int depth;
39
40 #ifdef DEBUG_TIMING
41   long count1, count2;
42   count1 = Counter();
43 #endif
44
45   /* load GIF file */
46   if (!(image = gifLoad(filename)))
47   {
48     printf("Loading GIF image failed -- maybe no GIF...\n");
49     exit(1);
50   }
51
52 #ifdef DEBUG_TIMING
53   count2 = Counter();
54   printf("   LOADING '%s' IN %.2f SECONDS\n",
55          filename, (float)(count2-count1)/1000.0);
56   count1 = Counter();
57 #endif
58
59   if (image->depth > 8)
60   {
61     printf("Sorry, GIFs with more than 256 colors are not supported.\n");
62     exit(1);
63   }
64
65   /* minimize colormap */
66   compress(image);
67
68 #ifdef DEBUG_TIMING
69   count2 = Counter();
70   printf("   COMPRESSING IMAGE COLORMAP IN %.2f SECONDS\n",
71          (float)(count2-count1)/1000.0);
72   count1 = Counter();
73 #endif
74
75   screen = DefaultScreen(display);
76   visual = DefaultVisual(display, screen);
77   depth = DefaultDepth(display, screen);
78
79   /* convert internal image structure to X11 XImage */
80   if (!(ximageinfo = imageToXImage(display, screen, visual, depth, image)))
81   {
82     fprintf(stderr, "Cannot convert Image to XImage.\n");
83     exit(1);
84   }
85
86 #ifdef DEBUG_TIMING
87   count2 = Counter();
88   printf("   CONVERTING IMAGE TO XIMAGE IN %.2f SECONDS\n",
89          (float)(count2-count1)/1000.0);
90   count1 = Counter();
91 #endif
92
93   if (ximageinfo->cmap != DefaultColormap(display, screen))
94     XSetWindowColormap(display, window, ximageinfo->cmap);
95
96   /* convert XImage to Pixmap */
97   if ((*pixmap = ximageToPixmap(display, window, ximageinfo)) == None)
98   {
99     fprintf(stderr, "Cannot convert XImage to Pixmap.\n");
100     exit(1);
101   }
102
103 #ifdef DEBUG_TIMING
104   count2 = Counter();
105   printf("   CONVERTING IMAGE TO PIXMAP IN %.2f SECONDS\n",
106          (float)(count2-count1)/1000.0);
107   count1 = Counter();
108 #endif
109
110   /* create mono image for masking */
111   image_mask = monochrome(image);
112
113 #ifdef DEBUG_TIMING
114   count2 = Counter();
115   printf("   CONVERTING IMAGE TO MASK IN %.2f SECONDS\n",
116          (float)(count2-count1)/1000.0);
117   count1 = Counter();
118 #endif
119
120   /* convert internal image structure to X11 XImage */
121   if (!(ximageinfo_mask = imageToXImage(display, screen, visual, depth,
122                                         image_mask)))
123   {
124     fprintf(stderr, "Cannot convert Image to XImage.\n");
125     exit(1);
126   }
127
128 #ifdef DEBUG_TIMING
129   count2 = Counter();
130   printf("   CONVERTING MASK TO XIMAGE IN %.2f SECONDS\n",
131          (float)(count2-count1)/1000.0);
132   count1 = Counter();
133 #endif
134
135   /* convert XImage to Pixmap */
136   if ((*pixmap_mask = ximageToPixmap(display, window, ximageinfo_mask)) == None)
137   {
138     fprintf(stderr, "Cannot convert XImage to Pixmap.\n");
139     exit(1);
140   }
141
142 #ifdef DEBUG_TIMING
143   count2 = Counter();
144   printf("   CONVERTING MASK TO PIXMAP IN %.2f SECONDS\n",
145          (float)(count2-count1)/1000.0);
146   count1 = Counter();
147 #endif
148
149   return(GIF_Success);
150 }
151
152 #endif