bmtoa.c revision eaef79e5
1/* $Xorg: bmtoa.c,v 1.4 2001/02/09 02:05:28 xorgcvs Exp $ */ 2/* $XdotOrg: $ */ 3/* 4 5Copyright 1988, 1993, 1998 The Open Group 6 7Permission to use, copy, modify, distribute, and sell this software and its 8documentation for any purpose is hereby granted without fee, provided that 9the above copyright notice appear in all copies and that both that 10copyright notice and this permission notice appear in supporting 11documentation. 12 13The above copyright notice and this permission notice shall be included 14in all copies or substantial portions of the Software. 15 16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR 20OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22OTHER DEALINGS IN THE SOFTWARE. 23 24Except as contained in this notice, the name of The Open Group shall 25not be used in advertising or otherwise to promote the sale, use or 26other dealings in this Software without prior written authorization 27from The Open Group. 28 29*/ 30/* $XFree86: xc/programs/bitmap/bmtoa.c,v 3.6 2001/07/25 15:05:12 dawes Exp $ */ 31 32/* 33 * bmtoa - bitmap to ascii filter 34 * Author: Jim Fulton, MIT X Consortium 35 */ 36 37#ifdef HAVE_CONFIG_H 38# include "config.h" 39#endif 40 41#include <stdio.h> 42#include <X11/Xlib.h> 43#include <X11/Xutil.h> 44#include <X11/Xos.h> 45 46#include <X11/Xmu/Drawing.h> 47 48#include <stdlib.h> 49#include <unistd.h> 50#ifndef HAS_MKSTEMP 51extern char *mktemp(); 52#endif 53 54char *ProgramName; 55 56static void print_scanline (unsigned int width, unsigned int height, 57 unsigned char *data, char *chars); 58 59static void 60usage (void) 61{ 62 fprintf (stderr, "usage: %s [-options ...] [filename]\n\n", 63 ProgramName); 64 fprintf (stderr, 65 "where options include:\n"); 66 fprintf (stderr, 67 " -chars cc chars to use for 0 and 1 bits, respectively\n"); 68 fprintf (stderr, "\n"); 69 exit (1); 70} 71 72static char * 73copy_stdin (void) 74{ 75#ifdef WIN32 76 static char tmpfilename[] = "/temp/bmtoa.XXXXXX"; 77#else 78 static char tmpfilename[] = "/tmp/bmtoa.XXXXXX"; 79#endif 80 char buf[BUFSIZ]; 81 FILE *fp; 82 int nread, nwritten; 83 84#ifndef HAS_MKSTEMP 85 if (mktemp (tmpfilename) == NULL) { 86 fprintf (stderr, 87 "%s: unable to genererate temporary file name for stdin.\n", 88 ProgramName); 89 exit (1); 90 } 91 fp = fopen (tmpfilename, "w"); 92#else 93 int fd; 94 95 if ((fd = mkstemp(tmpfilename)) < 0) { 96 fprintf (stderr, 97 "%s: unable to genererate temporary file name for stdin.\n", 98 ProgramName); 99 exit (1); 100 } 101 fp = fdopen(fd, "w"); 102#endif 103 while (1) { 104 buf[0] = '\0'; 105 nread = fread (buf, 1, sizeof buf, stdin); 106 if (nread <= 0) break; 107 nwritten = fwrite (buf, 1, nread, fp); 108 if (nwritten != nread) { 109 fprintf (stderr, 110 "%s: error copying stdin to file (%d of %d chars)\n", 111 ProgramName, nwritten, nread); 112 (void) fclose (fp); 113 (void) unlink (tmpfilename); 114 exit (1); 115 } 116 } 117 (void) fclose (fp); 118 return tmpfilename; 119} 120 121int 122main (int argc, char *argv[]) 123{ 124 char *filename = NULL; 125 int isstdin = 0; 126 char *chars = "-#"; 127 int i; 128 unsigned int width, height; 129 unsigned char *data; 130 int x_hot, y_hot; 131 int status; 132 133 ProgramName = argv[0]; 134 135 for (i = 1; i < argc; i++) { 136 char *arg = argv[i]; 137 138 if (arg[0] == '-') { 139 switch (arg[1]) { 140 case '\0': 141 filename = NULL; 142 continue; 143 case 'c': 144 if (++i >= argc) usage (); 145 chars = argv[i]; 146 continue; 147 default: 148 usage (); 149 } 150 } else { 151 filename = arg; 152 } 153 } 154 155 if (strlen (chars) != 2) { 156 fprintf (stderr, 157 "%s: bad character list \"%s\", must have exactly 2 characters\n", 158 ProgramName, chars); 159 exit (1); 160 } 161 162 if (!filename) { 163 filename = copy_stdin (); 164 isstdin = 1; 165 } 166 167 status = XmuReadBitmapDataFromFile (filename, &width, &height, &data, 168 &x_hot, &y_hot); 169 if (isstdin) (void) unlink (filename); /* don't need it anymore */ 170 if (status != BitmapSuccess) { 171 fprintf (stderr, "%s: unable to read bitmap from file \"%s\"\n", 172 ProgramName, isstdin ? "(stdin)" : filename); 173 exit (1); 174 } 175 176 print_scanline (width, height, data, chars); 177 exit (0); 178} 179 180static void 181print_scanline (unsigned int width, 182 unsigned int height, 183 unsigned char *data, 184 char *chars) 185{ 186 char *scanline = (char *) malloc (width + 1); 187 unsigned char *dp = data; 188 int row, column; 189 static unsigned char masktable[] = { 190 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; 191 int padded = ((width & 7) != 0); 192 193 if (!scanline) { 194 fprintf (stderr, "%s: unable to allocate %d bytes for scanline\n", 195 ProgramName, width + 1); 196 exit (1); 197 } 198 199 for (row = 0; row < height; row++) { 200 for (column = 0; column < width; column++) { 201 int i = (column & 7); 202 203 if (*dp & masktable[i]) { 204 putchar (chars[1]); 205 } else { 206 putchar (chars[0]); 207 } 208 209 if (i == 7) dp++; 210 } 211 putchar ('\n'); 212 if (padded) dp++; 213 } 214 return; 215} 216 217