7fd59977 |
1 | |
2 | /* |
3 | * Modified: zov : 21-Apr-1998 : Reads a BMP/GIF/XWD image from file |
4 | * |
5 | |
6 | XW_EXT_IMAGEDATA* Xw_load_image (awindow,aimageinfo,filename): |
7 | XW_EXT_WINDOW *awindow |
8 | XW_USERDATA *aimageinfo |
9 | char *filename - XWD/BMP/GIF file name |
10 | |
11 | Get an image from a file named filename. |
12 | |
13 | Returns the image descriptor address if successful, |
14 | or NULL if an error occured. |
15 | |
16 | Note! |
17 | It is strictly recommended to pass the filename with extension. |
18 | Otherwise an extension env(CSF_DefaultImageFormat) is appended. |
19 | In fact, it doesn't matter what extension has been used, because |
20 | file format will be automatically detected. (zov) |
21 | */ |
22 | |
23 | #define PRO16753 /* GG 261198 |
24 | // 1) Don't free pname because this pointer is a |
25 | // static address in Xw_get_filename() space. |
26 | // 2) open() C function return a int number < 0 |
27 | // when the open failed. |
28 | */ |
29 | |
30 | #include <Xw_Extension.h> |
31 | #ifdef HAVE_CONFIG_H |
32 | # include <config.h> |
33 | #endif |
34 | #include <fcntl.h> |
35 | #ifdef HAVE_STRINGS_H |
36 | # include <strings.h> |
37 | #endif |
38 | |
39 | #ifdef TRACE |
40 | #define TRACE_LOAD_IMAGE |
41 | #endif |
42 | |
43 | #ifdef XW_PROTOTYPE |
44 | XW_EXT_IMAGEDATA* Xw_load_xwd_image (void*, void*, char*, int, XColor**, int*); |
45 | XW_EXT_IMAGEDATA* Xw_load_bmp_image (void*, void*, char*, int, XColor**, int*); |
46 | XW_EXT_IMAGEDATA* Xw_load_gif_image (void*, void*, char*, int, XColor**, int*); |
47 | |
48 | void* Xw_load_image (void *awindow,void *aimageinfo,char *filename) |
49 | #else |
50 | XW_EXT_IMAGEDATA* Xw_load_xwd_image (); |
51 | XW_EXT_IMAGEDATA* Xw_load_bmp_image (); |
52 | XW_EXT_IMAGEDATA* Xw_load_gif_image (); |
53 | |
54 | void* Xw_load_image (awindow,aimageinfo,filename) |
55 | void *awindow; |
56 | void *aimageinfo; |
57 | char *filename ; |
58 | #endif /*XW_PROTOTYPE*/ |
59 | { |
60 | XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW *)awindow; |
61 | XW_EXT_IMAGEDATA *pimage; |
62 | XW_STATUS status; |
63 | XColor *pcolors = NULL; |
64 | int ncolors = 0, fimage = 0; |
65 | // char *wname = NULL, *pname, *pchDefExt, achHeader[8]; |
66 | char *pname, *pchDefExt, achHeader[8]; |
67 | |
68 | |
69 | if( !Xw_isdefine_window(pwindow) ) { |
70 | /*ERROR*Bad EXT_WINDOW Address*/ |
71 | Xw_set_error(24,"Xw_load_image",pwindow) ; |
72 | return (NULL) ; |
73 | } |
74 | |
75 | |
76 | /* Append desired extension to the file name if no ext. has been specified */ |
77 | pchDefExt = getenv ("CSF_DefaultImageFormat"); |
78 | pname = Xw_get_filename (filename,(char*)( pchDefExt? pchDefExt: "xwd")); |
79 | |
80 | |
81 | #ifdef DEBUG |
82 | fprintf (stderr, "\r\nXw_load_image: Loading '%s' (extended name: %s)...", |
83 | filename, pname? pname: "NULL"); |
84 | #endif /*DEBUG*/ |
85 | |
86 | |
87 | /* Open the file |
88 | * and detect the image format using signature |
89 | */ |
90 | if (pname) { |
91 | |
92 | static int _bFirstTime = 1; |
93 | if (_bFirstTime) { |
94 | |
95 | _bFirstTime = 0; |
96 | |
97 | if (!pchDefExt) |
98 | fprintf (stderr, |
99 | "\r\nWarning: variable CSF_DefaultImageFormat is undefined!" |
100 | " Assuming 'xwd'.\r\n"); |
101 | else if (strlen (pchDefExt) != 3 || |
102 | 0 != strcasecmp (pchDefExt, "xwd") |
103 | && 0 != strcasecmp (pchDefExt, "bmp") |
104 | && 0 != strcasecmp (pchDefExt, "gif")) |
105 | fprintf (stderr, |
106 | "\r\nWarning: value '%s' of CSF_DefaultImageFormat " |
107 | "is incorrect!\r\n", pchDefExt); |
108 | } |
109 | |
110 | fimage = open (pname, O_RDONLY); |
111 | |
112 | #ifndef PRO16753 |
113 | Xw_free (pname); |
114 | #endif |
115 | } |
116 | |
117 | #ifdef PRO16753 |
118 | if( fimage < 0 ) { |
119 | #else |
120 | if (!fimage) { |
121 | #endif |
122 | |
123 | fprintf (stderr, "\r\nXw_load_image: Error: " |
124 | "Can't open file '%s'!", filename); |
125 | return (NULL); |
126 | } |
127 | if (read (fimage, achHeader,sizeof (achHeader)) != sizeof (achHeader)) { |
128 | |
129 | fprintf (stderr, "\r\nXw_load_image: Error: " |
130 | "Can't read file '%s' to determine format!", filename); |
131 | close(fimage); |
132 | return (NULL); |
133 | } |
134 | |
135 | lseek (fimage, 0, SEEK_SET); |
136 | |
137 | |
138 | /* |
139 | * Detect file format; then load the image |
140 | * and obtain array of colors used |
141 | */ |
142 | if (0 == strncmp (achHeader, "GIF87a", 6)) |
143 | pimage = Xw_load_gif_image (pwindow, aimageinfo, filename, |
144 | fimage, &pcolors, &ncolors); |
145 | else if (0 == strncmp (achHeader, "GIF89a", 6)) { |
146 | |
147 | fprintf (stderr, |
148 | "\r\nXw_load_image: Warning: GIF89a format specified (file %s).", |
149 | filename); |
150 | pimage = Xw_load_gif_image (pwindow, aimageinfo, filename, |
151 | fimage, &pcolors, &ncolors); |
152 | } |
153 | else if (0 == strncmp (achHeader, "BM", 2)) |
154 | pimage = Xw_load_bmp_image (pwindow, aimageinfo, filename, |
155 | fimage, &pcolors, &ncolors); |
156 | else /* Then it must be XWD */ |
157 | pimage = Xw_load_xwd_image (pwindow, aimageinfo, filename, |
158 | fimage, &pcolors, &ncolors); |
159 | |
160 | close (fimage); |
161 | |
162 | |
163 | if (! pimage) { |
164 | |
165 | fprintf (stderr, "\r\nXw_load_image: Error: Failed to read %s!", |
166 | filename); |
167 | close(fimage); |
168 | return (NULL); |
169 | } |
170 | |
171 | |
172 | /* Color relocation: |
173 | * Converse image to what the window format supports. |
174 | */ |
175 | status = Xw_convert_image (pwindow, pimage, pcolors, ncolors); |
176 | |
177 | if (pcolors) |
178 | Xw_free(pcolors); |
179 | |
180 | if( status == XW_ERROR ) { |
181 | Xw_del_imagedata_structure (pimage) ; |
182 | return (NULL) ; |
183 | } |
184 | |
185 | |
186 | #ifdef TRACE_LOAD_IMAGE |
187 | if( Xw_get_trace() ) { |
188 | printf (" %lx = Xw_load_image(%lx,'%s')\n",(long ) pimage,(long ) pwindow,filename); |
189 | } |
190 | #endif |
191 | return (pimage); |
192 | } |