| 1 |
/* pbmtocmuwm.c - read a PBM image and produce a CMU window manager bitmap |
| 2 |
** |
| 3 |
** Copyright (C) 1989 by Jef Poskanzer. |
| 4 |
** |
| 5 |
** Permission to use, copy, modify, and distribute this software and its |
| 6 |
** documentation for any purpose and without fee is hereby granted, provided |
| 7 |
** that the above copyright notice appear in all copies and that both that |
| 8 |
** copyright notice and this permission notice appear in supporting |
| 9 |
** documentation. This software is provided "as is" without express or |
| 10 |
** implied warranty. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* 2006.10 (afu) |
| 14 |
Changed bitrow from plain to raw, read function from pbm_readpbmrow() to |
| 15 |
pbm_readpbmrow_packed(), write function from putc() to fwrite(). |
| 16 |
|
| 17 |
Retired bitwise transformation functions. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "pbm.h" |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
static void |
| 25 |
putinit(unsigned int const rows, |
| 26 |
unsigned int const cols) { |
| 27 |
|
| 28 |
const char initWriteError[] = |
| 29 |
"CMU window manager header write error"; |
| 30 |
uint32_t const cmuwmMagic = 0xf10040bb; |
| 31 |
|
| 32 |
int rc; |
| 33 |
|
| 34 |
rc = pm_writebiglong(stdout, cmuwmMagic); |
| 35 |
if (rc == -1) |
| 36 |
pm_error(initWriteError); |
| 37 |
rc = pm_writebiglong(stdout, cols); |
| 38 |
if (rc == -1) |
| 39 |
pm_error(initWriteError); |
| 40 |
rc = pm_writebiglong(stdout, rows); |
| 41 |
if (rc == -1) |
| 42 |
pm_error(initWriteError); |
| 43 |
rc = pm_writebigshort(stdout, (short) 1); |
| 44 |
if (rc == -1) |
| 45 |
pm_error(initWriteError); |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
int |
| 51 |
main(int argc, |
| 52 |
char * argv[]) { |
| 53 |
|
| 54 |
FILE * ifP; |
| 55 |
unsigned char * bitrow; |
| 56 |
int rows, cols; |
| 57 |
int format; |
| 58 |
unsigned int row; |
| 59 |
const char * inputFileName; |
| 60 |
|
| 61 |
pbm_init(&argc, argv); |
| 62 |
|
| 63 |
if (argc-1 > 1) |
| 64 |
pm_error("Too many arguments (%u). " |
| 65 |
"Only argument is optional input file", argc-1); |
| 66 |
if (argc-1 == 1) |
| 67 |
inputFileName = argv[1]; |
| 68 |
else |
| 69 |
inputFileName = "-"; |
| 70 |
|
| 71 |
ifP = pm_openr(inputFileName); |
| 72 |
|
| 73 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 74 |
bitrow = pbm_allocrow_packed(cols); |
| 75 |
|
| 76 |
putinit(rows, cols); |
| 77 |
|
| 78 |
/* Convert PBM raster data to CMUWM and write */ |
| 79 |
for (row = 0; row < rows; ++row) { |
| 80 |
unsigned int const bytesPerRow = pbm_packed_bytes(cols); |
| 81 |
unsigned char const padding = |
| 82 |
(cols % 8 == 0) ? 0x00 : ((unsigned char) ~0 >> (cols % 8)); |
| 83 |
|
| 84 |
unsigned int i; |
| 85 |
size_t bytesWritten; |
| 86 |
|
| 87 |
pbm_readpbmrow_packed(ifP, bitrow, cols, format); |
| 88 |
|
| 89 |
/* Invert all bits in row - raster formats are similar. |
| 90 |
PBM Black:1 White:0 "Don't care" bits at end of row |
| 91 |
CMUWM Black:0 White:1 End of row padded with 1 |
| 92 |
*/ |
| 93 |
|
| 94 |
for (i = 0; i < bytesPerRow; ++i) |
| 95 |
bitrow[i] = ~bitrow[i]; |
| 96 |
|
| 97 |
bitrow[bytesPerRow-1] |= padding; /* Set row end pad bits */ |
| 98 |
|
| 99 |
bytesWritten = fwrite(bitrow, 1, bytesPerRow, stdout); |
| 100 |
if (bytesWritten != bytesPerRow) |
| 101 |
pm_error("fwrite() failed to write CMU window manager bitmap"); |
| 102 |
} |
| 103 |
|
| 104 |
pm_close(ifP); |
| 105 |
return 0; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|