]>
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 /* Visual Age cannot deal with old, non-ansi, code */
56 XpmCreateDataFromImage(
61 , XpmAttributes
* attributes
65 XpmCreateDataFromImage(display
, data_return
, image
, shapeimage
, attributes
)
70 XpmAttributes
*attributes
;
77 /* initialize return value */
81 /* create an XpmImage from the image */
82 ErrorStatus
= XpmCreateXpmImageFromImage(display
, image
, shapeimage
,
83 &xpmimage
, attributes
);
84 if (ErrorStatus
!= XpmSuccess
)
87 /* create the data from the XpmImage */
89 xpmSetInfo(&info
, attributes
);
90 ErrorStatus
= XpmCreateDataFromXpmImage(data_return
, &xpmimage
, &info
);
92 ErrorStatus
= XpmCreateDataFromXpmImage(data_return
, &xpmimage
, NULL
);
94 /* free the XpmImage */
95 XpmFreeXpmImage(&xpmimage
);
101 #define RETURN(status) \
103 ErrorStatus = status; \
108 /* Visual Age cannot deal with old, non-ansi, code */
110 XpmCreateDataFromXpmImage(char*** data_return
, XpmImage
* image
, XpmInfo
* info
)
113 XpmCreateDataFromXpmImage(data_return
, image
, info
)
119 /* calculation variables */
122 char **header
= NULL
, **data
, **sptr
, **sptr2
, *s
;
123 unsigned int header_size
, header_nlines
;
124 unsigned int data_size
, data_nlines
;
125 unsigned int extensions
= 0, ext_size
= 0, ext_nlines
= 0;
126 unsigned int offset
, l
, n
;
130 extensions
= info
&& (info
->valuemask
& XpmExtensions
)
131 && info
->nextensions
;
133 /* compute the number of extensions lines and size */
135 CountExtensions(info
->extensions
, info
->nextensions
,
136 &ext_size
, &ext_nlines
);
139 * alloc a temporary array of char pointer for the header section which
140 * is the hints line + the color table lines
142 header_nlines
= 1 + image
->ncolors
;
143 header_size
= sizeof(char *) * header_nlines
;
144 header
= (char **) XpmCalloc(header_size
, sizeof(char *));
146 return (XpmNoMemory
);
148 /* print the hints line */
153 sprintf(s
, "%d %d %d %d", image
->width
, image
->height
,
154 image
->ncolors
, image
->cpp
);
159 if (info
&& (info
->valuemask
& XpmHotspot
)) {
163 sprintf(s
, " %d %d", info
->x_hotspot
, info
->y_hotspot
);
169 strcpy(s
, " XPMEXT");
173 *header
= (char *) XpmMalloc(l
);
177 strcpy(*header
, buf
);
180 ErrorStatus
= CreateColors(header
+ 1, &header_size
,
181 image
->colorTable
, image
->ncolors
, image
->cpp
);
183 if (ErrorStatus
!= XpmSuccess
)
186 /* now we know the size needed, alloc the data and copy the header lines */
187 offset
= image
->width
* image
->cpp
+ 1;
188 data_size
= header_size
+ (image
->height
+ ext_nlines
) * sizeof(char *)
189 + image
->height
* offset
+ ext_size
;
191 data
= (char **) XpmMalloc(data_size
);
195 data_nlines
= header_nlines
+ image
->height
+ ext_nlines
;
196 *data
= (char *) (data
+ data_nlines
);
198 for (l
= 0, sptr
= data
, sptr2
= header
; l
<= n
; l
++, sptr
++, sptr2
++) {
199 strcpy(*sptr
, *sptr2
);
200 *(sptr
+ 1) = *sptr
+ strlen(*sptr2
) + 1;
204 data
[header_nlines
] = (char *) data
+ header_size
205 + (image
->height
+ ext_nlines
) * sizeof(char *);
207 CreatePixels(data
+ header_nlines
, image
->width
, image
->height
,
208 image
->cpp
, image
->data
, image
->colorTable
);
210 /* print extensions */
212 CreateExtensions(data
+ header_nlines
+ image
->height
- 1, offset
,
213 info
->extensions
, info
->nextensions
,
217 ErrorStatus
= XpmSuccess
;
219 /* exit point, free only locally allocated variables */
222 for (l
= 0; l
< header_nlines
; l
++)
231 /* Visual Age cannot deal with old, non-ansi, code */
235 , unsigned int* data_size
237 , unsigned int ncolors
242 CreateColors(dataptr
, data_size
, colors
, ncolors
, cpp
)
244 unsigned int *data_size
;
246 unsigned int ncolors
;
251 unsigned int a
, key
, l
;
255 for (a
= 0; a
< ncolors
; a
++, colors
++, dataptr
++) {
257 defaults
= (char **) colors
;
258 strncpy(buf
, *defaults
++, cpp
);
261 for (key
= 1; key
<= NKEYS
; key
++, defaults
++) {
262 if (s2
= *defaults
) {
266 sprintf(s
, "\t%s %s", xpmColorKeys
[key
- 1], s2
);
273 s
= (char *) XpmMalloc(l
);
275 return (XpmNoMemory
);
277 *dataptr
= strcpy(s
, buf
);
283 /* Visual Age cannot deal with old, non-ansi, code */
284 static void CreatePixels(
287 , unsigned int height
289 , unsigned int* pixels
294 CreatePixels(dataptr
, width
, height
, cpp
, pixels
, colors
)
299 unsigned int *pixels
;
304 unsigned int x
, y
, h
, offset
;
307 offset
= width
* cpp
+ 1;
308 for (y
= 0; y
< h
; y
++, dataptr
++) {
310 for (x
= 0; x
< width
; x
++, pixels
++) {
311 strncpy(s
, colors
[*pixels
].string
, cpp
);
315 *(dataptr
+ 1) = *dataptr
+ offset
;
317 /* duplicate some code to avoid a test in the loop */
319 for (x
= 0; x
< width
; x
++, pixels
++) {
320 strncpy(s
, colors
[*pixels
].string
, cpp
);
327 /* Visual Age cannot deal with old, non-ansi, code */
328 static void CountExtensions(
331 , unsigned int* ext_size
332 , unsigned int* ext_nlines
336 CountExtensions(ext
, num
, ext_size
, ext_nlines
)
339 unsigned int *ext_size
;
340 unsigned int *ext_nlines
;
343 unsigned int x
, y
, a
, size
, nlines
;
348 for (x
= 0; x
< num
; x
++, ext
++) {
350 nlines
+= ext
->nlines
+ 1;
351 /* 8 = 7 (for "XPMEXT ") + 1 (for 0) */
352 size
+= strlen(ext
->name
) + 8;
354 for (y
= 0, line
= ext
->lines
; y
< a
; y
++, line
++)
355 size
+= strlen(*line
) + 1;
357 /* 10 and 1 are for the ending "XPMENDEXT" */
358 *ext_size
= size
+ 10;
359 *ext_nlines
= nlines
+ 1;
363 /* Visual Age cannot deal with old, non-ansi, code */
367 , unsigned int offset
370 , unsigned int ext_nlines
374 CreateExtensions(dataptr
, offset
, ext
, num
, ext_nlines
)
379 unsigned int ext_nlines
;
382 unsigned int x
, y
, a
, b
;
385 *(dataptr
+ 1) = *dataptr
+ offset
;
388 for (x
= 0; x
< num
; x
++, ext
++) {
389 sprintf(*dataptr
, "XPMEXT %s", ext
->name
);
392 *(dataptr
+ 1) = *dataptr
+ strlen(ext
->name
) + 8;
395 for (y
= 0, line
= ext
->lines
; y
< b
; y
++, line
++) {
396 strcpy(*dataptr
, *line
);
399 *(dataptr
+ 1) = *dataptr
+ strlen(*line
) + 1;
403 strcpy(*dataptr
, "XPMENDEXT");