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