| 1 |
/* pbmtozinc.c - read a PBM image and produce a bitmap file |
| 2 |
** in the format used by the Zinc Interface Library (v1.0) |
| 3 |
** November 1990. |
| 4 |
** |
| 5 |
** Author: James Darrell McCauley |
| 6 |
** Department of Agricultural Engineering |
| 7 |
** Texas A&M University |
| 8 |
** College Station, Texas 77843-2117 USA |
| 9 |
** |
| 10 |
** Copyright (C) 1988 by James Darrell McCauley ([email protected]) |
| 11 |
** and Jef Poskanzer. |
| 12 |
** |
| 13 |
** Permission to use, copy, modify, and distribute this software and its |
| 14 |
** documentation for any purpose and without fee is hereby granted, provided |
| 15 |
** that the above copyright notice appear in all copies and that both that |
| 16 |
** copyright notice and this permission notice appear in supporting |
| 17 |
** documentation. This software is provided "as is" without express or |
| 18 |
** implied warranty. |
| 19 |
*/ |
| 20 |
|
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "mallocvar.h" |
| 25 |
#include "nstring.h" |
| 26 |
#include "pbm.h" |
| 27 |
|
| 28 |
static void |
| 29 |
parseCommandLine(int const argc, |
| 30 |
const char ** const argv, |
| 31 |
const char ** const inputFileNameP) { |
| 32 |
|
| 33 |
if (argc-1 > 0) { |
| 34 |
*inputFileNameP = argv[1]; |
| 35 |
|
| 36 |
if (argc-1 > 1) |
| 37 |
pm_error("To many arguments: %u. " |
| 38 |
"The only possible argument is the " |
| 39 |
"name of the input file", argc-1); |
| 40 |
} else |
| 41 |
*inputFileNameP = "-"; |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
#pragma GCC diagnostic push |
| 47 |
#pragma GCC diagnostic ignored "-Wdangling-pointer" |
| 48 |
|
| 49 |
static const char * |
| 50 |
imageName(const char * const inputFileName) { |
| 51 |
/*---------------------------------------------------------------------------- |
| 52 |
The image name to put in the Zinc file, based on the input file name |
| 53 |
'inputFileName' ("-" to indicate Standard Input). |
| 54 |
|
| 55 |
Result is newly malloc'ed space that Caller must free. |
| 56 |
-----------------------------------------------------------------------------*/ |
| 57 |
const char * retval; |
| 58 |
|
| 59 |
if (streq(inputFileName, "-")) |
| 60 |
pm_asprintf(&retval, "noname"); |
| 61 |
else { |
| 62 |
char * nameBuf; |
| 63 |
|
| 64 |
MALLOCARRAY_NOFAIL(nameBuf, strlen(inputFileName) + 1); |
| 65 |
|
| 66 |
strcpy(nameBuf, inputFileName); |
| 67 |
|
| 68 |
{ |
| 69 |
/* Cut off the file name at the first period */ |
| 70 |
char * const periodP = strchr(nameBuf, '.' ); |
| 71 |
if (periodP) |
| 72 |
*periodP = '\0'; |
| 73 |
} |
| 74 |
retval = nameBuf; |
| 75 |
} |
| 76 |
return retval; |
| 77 |
} |
| 78 |
#pragma GCC diagnostic pop |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
typedef struct { |
| 83 |
unsigned int itemsperline; |
| 84 |
uint16_t item; |
| 85 |
unsigned int firstitem; |
| 86 |
} Packer; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
static void |
| 91 |
packer_init(Packer * const packerP) { |
| 92 |
|
| 93 |
packerP->itemsperline = 0; |
| 94 |
packerP->firstitem = 1; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
static void |
| 100 |
packer_putitem(Packer * const packerP, |
| 101 |
unsigned char const hi, |
| 102 |
unsigned char const lo) { |
| 103 |
|
| 104 |
if (packerP->firstitem) |
| 105 |
packerP->firstitem = 0; |
| 106 |
else |
| 107 |
putchar(','); |
| 108 |
|
| 109 |
if (packerP->itemsperline == 11) { |
| 110 |
putchar('\n'); |
| 111 |
packerP->itemsperline = 0; |
| 112 |
} |
| 113 |
if (packerP->itemsperline == 0) |
| 114 |
putchar(' '); |
| 115 |
|
| 116 |
++packerP->itemsperline; |
| 117 |
|
| 118 |
printf ("0x%02x%02x", hi, lo); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
static void |
| 125 |
writeRaster(FILE * const ifP, |
| 126 |
unsigned int const rows, |
| 127 |
unsigned int const cols, |
| 128 |
int const format) { |
| 129 |
|
| 130 |
unsigned char * const bitrow = pbm_allocrow_packed(cols + 8); |
| 131 |
|
| 132 |
Packer packer; |
| 133 |
unsigned int row; |
| 134 |
|
| 135 |
packer_init(&packer); |
| 136 |
|
| 137 |
bitrow[pbm_packed_bytes(cols+8) -1 ] = 0x00; |
| 138 |
|
| 139 |
for (row = 0; row < rows; ++row) { |
| 140 |
unsigned int const itemCt = (cols + 15 ) / 16; |
| 141 |
|
| 142 |
unsigned int i; |
| 143 |
|
| 144 |
pbm_readpbmrow_packed(ifP, bitrow, cols, format); |
| 145 |
|
| 146 |
pbm_cleanrowend_packed(bitrow, cols); |
| 147 |
|
| 148 |
for (i = 0; i < itemCt; ++i) { |
| 149 |
packer_putitem(&packer, bitrow[2*i+0], bitrow[2*i+1]); |
| 150 |
} |
| 151 |
} |
| 152 |
pbm_freerow_packed(bitrow); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
int |
| 158 |
main(int argc, const char * argv[]) { |
| 159 |
|
| 160 |
const char * inputFileName; |
| 161 |
FILE * ifP; |
| 162 |
int rows, cols; |
| 163 |
int format; |
| 164 |
const char * name; |
| 165 |
|
| 166 |
pm_proginit(&argc, argv); |
| 167 |
|
| 168 |
parseCommandLine(argc, argv, &inputFileName); |
| 169 |
|
| 170 |
ifP = pm_openr(inputFileName); |
| 171 |
|
| 172 |
name = imageName(inputFileName); |
| 173 |
|
| 174 |
pbm_readpbminit(ifP, &cols, &rows, &format); |
| 175 |
|
| 176 |
printf("USHORT %s[] = {\n", name); |
| 177 |
printf(" %d\n", cols); |
| 178 |
printf(" %d\n", rows); |
| 179 |
|
| 180 |
writeRaster(ifP, rows, cols, format); |
| 181 |
|
| 182 |
printf("};\n"); |
| 183 |
|
| 184 |
pm_close(ifP); |
| 185 |
|
| 186 |
pm_strfree(name); |
| 187 |
|
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|