WrBitF.c revision 1ab64890
1/* $Xorg: WrBitF.c,v 1.4 2001/02/09 02:03:37 xorgcvs Exp $ */
2/*
3
4Copyright 1987, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included
13in all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall
24not be used in advertising or otherwise to promote the sale, use or
25other dealings in this Software without prior written authorization
26from The Open Group.
27
28*/
29/* $XFree86: xc/lib/X11/WrBitF.c,v 3.6 2003/04/13 19:22:18 dawes Exp $ */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include "Xlibint.h"
35#include <X11/Xos.h>
36#include "Xutil.h"
37#include <stdio.h>
38
39#define ERR_RETURN NULL
40
41static char *Format_Image(
42    XImage *image,
43    int *resultsize)
44{
45  register int x, c, b;
46  register char *ptr;
47  int y;
48  char *data;
49  int width, height;
50  int bytes_per_line;
51
52  width = image->width;
53  height = image->height;
54
55  bytes_per_line = (width+7)/8;
56  *resultsize = bytes_per_line * height;           /* Calculate size of data */
57
58  data = (char *) Xmalloc( *resultsize );           /* Get space for data */
59  if (!data)
60    return(ERR_RETURN);
61
62  /*
63   * The slow but robust brute force method of converting the image:
64   */
65  ptr = data;
66  c = 0; b=1;
67  for (y=0; y<height; y++) {
68    for (x=0; x<width;) {
69      if (XGetPixel(image, x, y))
70	c |= b;
71      b <<= 1;
72      if (!(++x & 7)) {
73	*(ptr++)=c;
74	c=0; b=1;
75      }
76    }
77    if (x & 7) {
78      *(ptr++)=c;
79      c=0; b=1;
80    }
81  }
82
83  return(data);
84}
85
86#define BYTES_PER_OUTPUT_LINE 12
87
88int
89XWriteBitmapFile(
90     Display *display,
91     _Xconst char *filename,
92     Pixmap bitmap,
93     unsigned int width,
94     unsigned int height,
95     int x_hot,
96     int y_hot)
97{
98  char *data, *ptr;
99  int size, byte;
100  int c;
101  XImage *image;
102  FILE *stream;
103  char *name;
104
105  if (!(name = strrchr(filename, '/')))
106    name = (char *)filename;
107  else
108    name++;
109
110#ifdef __UNIXOS2__
111  filename = (char*)__XOS2RedirRoot(filename);
112#endif
113  if (!(stream = fopen(filename, "w")))
114    return(BitmapOpenFailed);
115
116  /* Convert bitmap to an image */
117  image = XGetImage(display, bitmap, 0,0,width, height, 1L, XYPixmap);
118  if (!image) {
119    fclose(stream);
120    return(4); /* XXX spec does not say what to return */
121  }
122
123  /* Get standard format for data */
124  data = Format_Image(image, &size);
125  XDestroyImage(image);
126  if (!data) {
127    fclose(stream);
128    return(BitmapNoMemory);
129  }
130
131  /* Write out standard header */
132  fprintf(stream, "#define %s_width %d\n", name, width);
133  fprintf(stream, "#define %s_height %d\n", name, height);
134  if (x_hot != -1) {
135    fprintf(stream, "#define %s_x_hot %d\n", name, x_hot);
136    fprintf(stream, "#define %s_y_hot %d\n", name, y_hot);
137  }
138
139  /* Print out the data itself */
140  fprintf(stream, "static unsigned char %s_bits[] = {", name);
141  for (byte=0, ptr=data; byte<size; byte++, ptr++) {
142    if (!byte)
143      fprintf(stream, "\n   ");
144    else if (!(byte % BYTES_PER_OUTPUT_LINE))
145      fprintf(stream, ",\n   ");
146    else
147      fprintf(stream, ", ");
148    c = *ptr;
149    if (c<0)
150      c += 256;
151    fprintf(stream, "0x%02x", c);
152  }
153  fprintf(stream, "};\n");
154
155  Xfree(data);
156  fclose(stream);
157  return(BitmapSuccess);
158}
159