]>
git.saurik.com Git - wxWidgets.git/blob - src/xpm/crdatfri.c
2 * Copyright (C) 1989-95 GROUPE BULL
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to
6 * deal in the Software without restriction, including without limitation the
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 * sell copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * GROUPE BULL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 * Except as contained in this notice, the name of GROUPE BULL shall not be
22 * used in advertising or otherwise to promote the sale, use or other dealings
23 * in this Software without prior written authorization from GROUPE BULL.
26 /*****************************************************************************\
30 * Scan an image and possibly its mask and create an XPM array *
32 * Developed by Arnaud Le Hors *
33 \*****************************************************************************/
37 LFUNC(CreateColors
, int, (char **dataptr
, unsigned int *data_size
,
38 XpmColor
*colors
, unsigned int ncolors
,
41 LFUNC(CreatePixels
, void, (char **dataptr
, unsigned int width
,
42 unsigned int height
, unsigned int cpp
,
43 unsigned int *pixels
, XpmColor
*colors
));
45 LFUNC(CountExtensions
, void, (XpmExtension
*ext
, unsigned int num
,
46 unsigned int *ext_size
,
47 unsigned int *ext_nlines
));
49 LFUNC(CreateExtensions
, void, (char **dataptr
, unsigned int offset
,
50 XpmExtension
*ext
, unsigned int num
,
51 unsigned int ext_nlines
));
54 XpmCreateDataFromImage(display
, data_return
, image
, shapeimage
, attributes
)
59 XpmAttributes
*attributes
;
65 /* initialize return value */
69 /* create an XpmImage from the image */
70 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
71 &xpmimage
, attributes
);
72 if (ErrorStatus
!= XpmSuccess
)
75 /* create the data from the XpmImage */
77 xpmSetInfo(&info
, attributes
);
78 ErrorStatus
= XpmCreateDataFromXpmImage(data_return
, &xpmimage
, &info
);
80 ErrorStatus
= XpmCreateDataFromXpmImage(data_return
, &xpmimage
, NULL
);
82 /* free the XpmImage */
83 XpmFreeXpmImage(&xpmimage
);
89 #define RETURN(status) \
91 ErrorStatus = status; \
96 XpmCreateDataFromXpmImage(data_return
, image
, info
)
101 /* calculation variables */
104 char **header
= NULL
, **data
, **sptr
, **sptr2
, *s
;
105 unsigned int header_size
, header_nlines
;
106 unsigned int data_size
, data_nlines
;
107 unsigned int extensions
= 0, ext_size
= 0, ext_nlines
= 0;
108 unsigned int offset
, l
, n
;
112 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
113 && info
->nextensions
;
115 /* compute the number of extensions lines and size */
117 CountExtensions(info
->extensions
, info
->nextensions
,
118 &ext_size
, &ext_nlines
);
121 * alloc a temporary array of char pointer for the header section which
122 * is the hints line + the color table lines
124 header_nlines
= 1 + image
->ncolors
;
125 header_size
= sizeof(char *) * header_nlines
;
126 header
= (char **) XpmCalloc(header_size
, sizeof(char *));
128 return (XpmNoMemory
);
130 /* print the hints line */
135 sprintf(s
, "%d %d %d %d", image
->width
, image
->height
,
136 image
->ncolors
, image
->cpp
);
141 if (info
&& (info
->valuemask
& XpmHotspot
)) {
145 sprintf(s
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
151 strcpy(s
, " XPMEXT");
155 *header
= (char *) XpmMalloc(l
);
159 strcpy(*header
, buf
);
162 ErrorStatus
= CreateColors(header
+ 1, &header_size
,
163 image
->colorTable
, image
->ncolors
, image
->cpp
);
165 if (ErrorStatus
!= XpmSuccess
)
168 /* now we know the size needed, alloc the data and copy the header lines */
169 offset
= image
->width
* image
->cpp
+ 1;
170 data_size
= header_size
+ (image
->height
+ ext_nlines
) * sizeof(char *)
171 + image
->height
* offset
+ ext_size
;
173 data
= (char **) XpmMalloc(data_size
);
177 data_nlines
= header_nlines
+ image
->height
+ ext_nlines
;
178 *data
= (char *) (data
+ data_nlines
);
180 for (l
= 0, sptr
= data
, sptr2
= header
; l
<= n
; l
++, sptr
++, sptr2
++) {
181 strcpy(*sptr
, *sptr2
);
182 *(sptr
+ 1) = *sptr
+ strlen(*sptr2
) + 1;
186 data
[header_nlines
] = (char *) data
+ header_size
187 + (image
->height
+ ext_nlines
) * sizeof(char *);
189 CreatePixels(data
+ header_nlines
, image
->width
, image
->height
,
190 image
->cpp
, image
->data
, image
->colorTable
);
192 /* print extensions */
194 CreateExtensions(data
+ header_nlines
+ image
->height
- 1, offset
,
195 info
->extensions
, info
->nextensions
,
199 ErrorStatus
= XpmSuccess
;
201 /* exit point, free only locally allocated variables */
204 for (l
= 0; l
< header_nlines
; l
++)
213 CreateColors(dataptr
, data_size
, colors
, ncolors
, cpp
)
215 unsigned int *data_size
;
217 unsigned int ncolors
;
221 unsigned int a
, key
, l
;
225 for (a
= 0; a
< ncolors
; a
++, colors
++, dataptr
++) {
227 defaults
= (char **) colors
;
228 strncpy(buf
, *defaults
++, cpp
);
231 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
232 if (s2
= *defaults
) {
236 sprintf(s
, "\t%s %s", xpmColorKeys
[key
- 1], s2
);
243 s
= (char *) XpmMalloc(l
);
245 return (XpmNoMemory
);
247 *dataptr
= strcpy(s
, buf
);
253 CreatePixels(dataptr
, width
, height
, cpp
, pixels
, colors
)
258 unsigned int *pixels
;
262 unsigned int x
, y
, h
, offset
;
265 offset
= width
* cpp
+ 1;
266 for (y
= 0; y
< h
; y
++, dataptr
++) {
268 for (x
= 0; x
< width
; x
++, pixels
++) {
269 strncpy(s
, colors
[*pixels
].string
, cpp
);
273 *(dataptr
+ 1) = *dataptr
+ offset
;
275 /* duplicate some code to avoid a test in the loop */
277 for (x
= 0; x
< width
; x
++, pixels
++) {
278 strncpy(s
, colors
[*pixels
].string
, cpp
);
285 CountExtensions(ext
, num
, ext_size
, ext_nlines
)
288 unsigned int *ext_size
;
289 unsigned int *ext_nlines
;
291 unsigned int x
, y
, a
, size
, nlines
;
296 for (x
= 0; x
< num
; x
++, ext
++) {
298 nlines
+= ext
->nlines
+ 1;
299 /* 8 = 7 (for "XPMEXT ") + 1 (for 0) */
300 size
+= strlen(ext
->name
) + 8;
302 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++)
303 size
+= strlen(*line
) + 1;
305 /* 10 and 1 are for the ending "XPMENDEXT" */
306 *ext_size
= size
+ 10;
307 *ext_nlines
= nlines
+ 1;
311 CreateExtensions(dataptr
, offset
, ext
, num
, ext_nlines
)
316 unsigned int ext_nlines
;
318 unsigned int x
, y
, a
, b
;
321 *(dataptr
+ 1) = *dataptr
+ offset
;
324 for (x
= 0; x
< num
; x
++, ext
++) {
325 sprintf(*dataptr
, "XPMEXT %s", ext
->name
);
328 *(dataptr
+ 1) = *dataptr
+ strlen(ext
->name
) + 8;
331 for (y
= 0, line
= ext
->lines
; y
< b
; y
++, line
++) {
332 strcpy(*dataptr
, *line
);
335 *(dataptr
+ 1) = *dataptr
+ strlen(*line
) + 1;
339 strcpy(*dataptr
, "XPMENDEXT");