| 1 |
/*---------------------------------------------------------------------------- |
| 2 |
pamtompfont |
| 3 |
------------------------------------------------------------------------------ |
| 4 |
Part of the Netpbm package. |
| 5 |
|
| 6 |
Convert a PAM image to an Mplayer bitmap font. |
| 7 |
|
| 8 |
It is obvious that this format was designed to be an image format and |
| 9 |
adopted by Mplayer for it's fonts (before Mplayer got the ability to |
| 10 |
use Freetype to read standard font formats such as TrueType). But |
| 11 |
I have no idea what the format was originally. |
| 12 |
|
| 13 |
In the Mplayer font subset of the format, the image is always grayscale |
| 14 |
(one byte per pixel) with no palette. |
| 15 |
|
| 16 |
By Bryan Henderson, San Jose CA 2008.05.18 |
| 17 |
|
| 18 |
Contributed to the public domain by its author. |
| 19 |
-----------------------------------------------------------------------------*/ |
| 20 |
|
| 21 |
#include <string.h> |
| 22 |
#include <assert.h> |
| 23 |
|
| 24 |
#include "pm_c_util.h" |
| 25 |
#include "mallocvar.h" |
| 26 |
#include "shhopt.h" |
| 27 |
#include "pm.h" |
| 28 |
#include "pam.h" |
| 29 |
|
| 30 |
struct cmdlineInfo { |
| 31 |
/* All the information the user supplied in the command line, |
| 32 |
in a form easy for the program to use. |
| 33 |
*/ |
| 34 |
const char * inputFilename; |
| 35 |
}; |
| 36 |
|
| 37 |
|
| 38 |
static void |
| 39 |
parseCommandLine(int argc, char ** argv, |
| 40 |
struct cmdlineInfo *cmdlineP) { |
| 41 |
/*---------------------------------------------------------------------------- |
| 42 |
Note that the file spec array we return is stored in the storage that |
| 43 |
was passed to us as the argv array. |
| 44 |
-----------------------------------------------------------------------------*/ |
| 45 |
optEntry * option_def; |
| 46 |
/* Instructions to OptParseOptions3 on how to parse our options. |
| 47 |
*/ |
| 48 |
optStruct3 opt; |
| 49 |
|
| 50 |
unsigned int option_def_index; |
| 51 |
|
| 52 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 53 |
|
| 54 |
option_def_index = 0; /* incremented by OPTENTRY */ |
| 55 |
|
| 56 |
OPTENTINIT; |
| 57 |
|
| 58 |
opt.opt_table = option_def; |
| 59 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 60 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 61 |
|
| 62 |
pm_optParseOptions3(&argc, argv, opt, sizeof(opt), 0); |
| 63 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 64 |
|
| 65 |
if (argc-1 == 0) |
| 66 |
cmdlineP->inputFilename = "-"; |
| 67 |
else if (argc-1 != 1) |
| 68 |
pm_error("Program takes zero or one argument (filename). You " |
| 69 |
"specified %d", argc-1); |
| 70 |
else |
| 71 |
cmdlineP->inputFilename = argv[1]; |
| 72 |
|
| 73 |
free(option_def); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
static void |
| 79 |
validateInput(struct pam * const inpamP) { |
| 80 |
|
| 81 |
/* The image format does provide for RGB images, but Mplayer doesn't |
| 82 |
understand that format (and doesn't even recognize it as something |
| 83 |
it doesn't understand) |
| 84 |
*/ |
| 85 |
|
| 86 |
if (inpamP->depth != 1) |
| 87 |
pm_error("Input must have depth 1. This image's depth is %u", |
| 88 |
inpamP->depth); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
static void |
| 94 |
writeMpFontHeader(FILE * const ofP, |
| 95 |
struct pam * const inpamP) { |
| 96 |
/*---------------------------------------------------------------------------- |
| 97 |
Write the 32 byte header. |
| 98 |
-----------------------------------------------------------------------------*/ |
| 99 |
fwrite("mhwanh", 1, 6, ofP); /* Signature */ |
| 100 |
|
| 101 |
fputc(0, ofP); /* pad */ |
| 102 |
fputc(0, ofP); /* pad */ |
| 103 |
|
| 104 |
/* Write the old 16 bit width field. Zero means use the 32 bit one |
| 105 |
below instead. |
| 106 |
*/ |
| 107 |
pm_writebigshort(ofP, 0); |
| 108 |
|
| 109 |
/* Height */ |
| 110 |
pm_writebigshort(ofP, inpamP->height); |
| 111 |
|
| 112 |
/* Number of colors in palette. Zero means not paletted image */ |
| 113 |
pm_writebigshort(ofP, 0); |
| 114 |
|
| 115 |
{ |
| 116 |
unsigned int i; |
| 117 |
for (i = 0; i < 14; ++i) |
| 118 |
fputc(0, ofP); /* pad */ |
| 119 |
} |
| 120 |
/* Width */ |
| 121 |
pm_writebiglong(ofP, inpamP->width); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
static void |
| 127 |
convertRaster(struct pam * const inpamP, |
| 128 |
FILE * const ofP) { |
| 129 |
|
| 130 |
tuple * tuplerow; |
| 131 |
unsigned char * outrow; |
| 132 |
unsigned int row; |
| 133 |
|
| 134 |
assert(inpamP->depth == 1); |
| 135 |
|
| 136 |
tuplerow = pnm_allocpamrow(inpamP); |
| 137 |
|
| 138 |
MALLOCARRAY(outrow, inpamP->width); |
| 139 |
|
| 140 |
if (outrow == NULL) |
| 141 |
pm_error("Unable to allocate space for a %u-column output buffer", |
| 142 |
inpamP->width); |
| 143 |
|
| 144 |
for (row = 0; row < inpamP->height; ++row) { |
| 145 |
unsigned int col; |
| 146 |
|
| 147 |
pnm_readpamrow(inpamP, tuplerow); |
| 148 |
|
| 149 |
for (col = 0; col < inpamP->width; ++col) { |
| 150 |
outrow[col] = |
| 151 |
pnm_scalesample(tuplerow[col][0], inpamP->maxval, 255); |
| 152 |
} |
| 153 |
|
| 154 |
fwrite(outrow, 1, inpamP->width, ofP); |
| 155 |
} |
| 156 |
free(outrow); |
| 157 |
pnm_freepamrow(tuplerow); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
int |
| 163 |
main(int argc, char *argv[]) { |
| 164 |
|
| 165 |
struct cmdlineInfo cmdline; |
| 166 |
FILE * ifP; |
| 167 |
struct pam inpam; /* Input PAM image */ |
| 168 |
|
| 169 |
pnm_init(&argc, argv); |
| 170 |
|
| 171 |
parseCommandLine(argc, argv, &cmdline); |
| 172 |
|
| 173 |
ifP = pm_openr(cmdline.inputFilename); |
| 174 |
|
| 175 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 176 |
|
| 177 |
validateInput(&inpam); |
| 178 |
|
| 179 |
writeMpFontHeader(stdout, &inpam); |
| 180 |
|
| 181 |
convertRaster(&inpam, stdout); |
| 182 |
|
| 183 |
return 0; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|