summaryrefslogtreecommitdiff
path: root/src/Xw/Xw_load_image.cxx
blob: a803e2a6cd198de63d93615dee6998c255e1e613 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

/*
 * Modified:  zov : 21-Apr-1998  : Reads a BMP/GIF/XWD image from file
 *

   XW_EXT_IMAGEDATA* Xw_load_image (awindow,aimageinfo,filename):
   XW_EXT_WINDOW  *awindow
   XW_USERDATA    *aimageinfo
   char           *filename     - XWD/BMP/GIF file name

   Get an image from a file named filename.

   Returns the image descriptor address if successful,
	         or  NULL if an error occured.

   Note!
   It is strictly recommended to pass the filename with extension.
   Otherwise an extension env(CSF_DefaultImageFormat) is appended.
   In fact, it doesn't matter what extension has been used, because
   file format will be automatically detected. (zov)
*/

#define PRO16753       /* GG 261198
//              1) Don't free pname because this pointer is a
//              static address in Xw_get_filename() space.
//              2) open() C function return a int number < 0
//              when the open failed.
*/

#include <Xw_Extension.h>
#ifdef HAVE_CONFIG_H
# include <oce-config.h>
#endif
#include <fcntl.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#ifdef TRACE
#define TRACE_LOAD_IMAGE
#endif

#ifdef XW_PROTOTYPE
XW_EXT_IMAGEDATA* Xw_load_xwd_image (void*, void*, char*, int, XColor**, int*);
XW_EXT_IMAGEDATA* Xw_load_bmp_image (void*, void*, char*, int, XColor**, int*);
XW_EXT_IMAGEDATA* Xw_load_gif_image (void*, void*, char*, int, XColor**, int*);

void* Xw_load_image (void *awindow,void *aimageinfo,char *filename)
#else
XW_EXT_IMAGEDATA* Xw_load_xwd_image ();
XW_EXT_IMAGEDATA* Xw_load_bmp_image ();
XW_EXT_IMAGEDATA* Xw_load_gif_image ();

void* Xw_load_image (awindow,aimageinfo,filename)
     void *awindow;
     void *aimageinfo;
     char *filename ;
#endif /*XW_PROTOTYPE*/
{
  XW_EXT_WINDOW *pwindow = (XW_EXT_WINDOW *)awindow;
  XW_EXT_IMAGEDATA *pimage;
  XW_STATUS status;
  XColor *pcolors = NULL;
  int ncolors = 0, fimage = 0;
  //  char *wname = NULL, *pname, *pchDefExt, achHeader[8];
  char *pname, *pchDefExt, achHeader[8];


  if( !Xw_isdefine_window(pwindow) ) {
    /*ERROR*Bad EXT_WINDOW Address*/
    Xw_set_error(24,"Xw_load_image",pwindow) ;
    return (NULL) ;
  }


  /* Append desired extension to the file name if no ext. has been specified */
  pchDefExt = getenv ("CSF_DefaultImageFormat");
  pname = Xw_get_filename (filename,(char*)( pchDefExt? pchDefExt: "xwd"));

  
#ifdef DEBUG
  fprintf (stderr, "\r\nXw_load_image: Loading '%s' (extended name: %s)...",
	   filename, pname? pname: "NULL");
#endif /*DEBUG*/
  

  /* Open the file
   * and detect the image format using signature
   */
  if (pname) {

    static int _bFirstTime = 1;
    if (_bFirstTime) {

      _bFirstTime = 0;

      if (!pchDefExt)
        fprintf (stderr,
		 "\r\nWarning: variable CSF_DefaultImageFormat is undefined!"
		 " Assuming 'xwd'.\r\n");
      else if (strlen (pchDefExt) != 3 ||
	         (0 != strcasecmp (pchDefExt, "xwd")
	       && 0 != strcasecmp (pchDefExt, "bmp")
	       && 0 != strcasecmp (pchDefExt, "gif")))
        fprintf (stderr,
		 "\r\nWarning: value '%s' of CSF_DefaultImageFormat "
		 "is incorrect!\r\n", pchDefExt);
    }

    fimage = open (pname, O_RDONLY);

#ifndef PRO16753
    Xw_free (pname);
#endif
  }

#ifdef PRO16753
  if( fimage < 0 ) {
#else
  if (!fimage) {
#endif

    fprintf (stderr, "\r\nXw_load_image: Error: "
	     "Can't open file '%s'!", filename);
    return (NULL);
  }
  if (read (fimage, achHeader,sizeof (achHeader)) != sizeof (achHeader)) {

    fprintf (stderr, "\r\nXw_load_image: Error: "
	     "Can't read file '%s' to determine format!", filename);
    close(fimage);
    return (NULL);
  }

  lseek (fimage, 0, SEEK_SET);


  /*
   * Detect file format; then load the image
   * and obtain array of colors used
   */
  if (0 == strncmp (achHeader, "GIF87a", 6))
    pimage = Xw_load_gif_image (pwindow, aimageinfo, filename,
				fimage, &pcolors, &ncolors);
  else if (0 == strncmp (achHeader, "GIF89a", 6)) {

    fprintf (stderr, 
	     "\r\nXw_load_image: Warning: GIF89a format specified (file %s).",
	     filename);
    pimage = Xw_load_gif_image (pwindow, aimageinfo, filename,
				fimage, &pcolors, &ncolors);
  }
  else if (0 == strncmp (achHeader, "BM", 2))
    pimage = Xw_load_bmp_image (pwindow, aimageinfo, filename,
				fimage, &pcolors, &ncolors);
  else /* Then it must be XWD */
    pimage = Xw_load_xwd_image (pwindow, aimageinfo, filename,
				fimage, &pcolors, &ncolors);

  close (fimage);


  if (! pimage) {

    fprintf (stderr, "\r\nXw_load_image: Error: Failed to read %s!",
	     filename);
    close(fimage);
    return (NULL);
  }


  /* Color relocation:
   * Converse image to what the window format supports.
   */
  status = Xw_convert_image (pwindow, pimage, pcolors, ncolors);

  if (pcolors)
    Xw_free(pcolors);

  if( status == XW_ERROR ) {
    Xw_del_imagedata_structure (pimage) ;
    return (NULL) ;
  }


#ifdef  TRACE_LOAD_IMAGE
  if( Xw_get_trace() ) {
    printf (" %lx = Xw_load_image(%lx,'%s')\n",(long ) pimage,(long ) pwindow,filename);
  }
#endif
  return (pimage);
}