| 1 |
/* |
| 2 |
* Convert a Netpbm image to SRF (Garmin vehicle) |
| 3 |
* |
| 4 |
* Copyright (C) 2011 by Mike Frysinger <[email protected]> |
| 5 |
* |
| 6 |
* Permission to use, copy, modify, and distribute this software and its |
| 7 |
* documentation for any purpose and without fee is hereby granted, provided |
| 8 |
* that the above copyright notice appear in all copies and that both that |
| 9 |
* copyright notice and this permission notice appear in supporting |
| 10 |
* documentation. This software is provided "as is" without express or |
| 11 |
* implied warranty. |
| 12 |
*/ |
| 13 |
|
| 14 |
#include <stdio.h> |
| 15 |
|
| 16 |
#include "pm_c_util.h" |
| 17 |
#include "shhopt.h" |
| 18 |
#include "mallocvar.h" |
| 19 |
#include "nstring.h" |
| 20 |
#include "pam.h" |
| 21 |
#include "srf.h" |
| 22 |
|
| 23 |
struct cmdlineInfo { |
| 24 |
/* All the information the user supplied in the command line, |
| 25 |
in a form easy for the program to use. |
| 26 |
*/ |
| 27 |
const char * inputFileName; /* '-' if stdin */ |
| 28 |
unsigned int verbose; |
| 29 |
}; |
| 30 |
|
| 31 |
static bool verbose; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static void |
| 36 |
parseCommandLine(int argc, const char ** argv, |
| 37 |
struct cmdlineInfo * const cmdlineP) { |
| 38 |
/*---------------------------------------------------------------------------- |
| 39 |
parse program command line described in Unix standard form by argc |
| 40 |
and argv. Return the information in the options as *cmdlineP. |
| 41 |
|
| 42 |
If command line is internally inconsistent (invalid options, etc.), |
| 43 |
issue error message to stderr and abort program. |
| 44 |
|
| 45 |
Note that the strings we return are stored in the storage that |
| 46 |
was passed to us as the argv array. We also trash *argv. |
| 47 |
-----------------------------------------------------------------------------*/ |
| 48 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 49 |
optStruct3 opt; |
| 50 |
|
| 51 |
unsigned int option_def_index; |
| 52 |
|
| 53 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 54 |
|
| 55 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 56 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 57 |
&cmdlineP->verbose, 0); |
| 58 |
|
| 59 |
opt.opt_table = option_def; |
| 60 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 61 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 62 |
|
| 63 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 64 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 65 |
|
| 66 |
if (argc-1 < 1) |
| 67 |
cmdlineP->inputFileName = "-"; |
| 68 |
else if (argc-1 == 1) |
| 69 |
cmdlineP->inputFileName = argv[1]; |
| 70 |
else |
| 71 |
pm_error("Program takes at most one argument: input file name"); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
static uint8_t |
| 77 |
srfScale(sample const unscaled, |
| 78 |
const struct pam * const pamP) { |
| 79 |
|
| 80 |
return pnm_scalesample(unscaled, pamP->maxval, 255); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static uint16_t |
| 86 |
srfColorFromTuple(tuple const t, |
| 87 |
const struct pam * const pamP) { |
| 88 |
|
| 89 |
unsigned int redPlane, grnPlane, bluPlane; |
| 90 |
|
| 91 |
if (pamP->depth >= 3) { |
| 92 |
redPlane = PAM_RED_PLANE; |
| 93 |
grnPlane = PAM_GRN_PLANE; |
| 94 |
bluPlane = PAM_BLU_PLANE; |
| 95 |
} else { |
| 96 |
redPlane = 0; |
| 97 |
grnPlane = 0; |
| 98 |
bluPlane = 0; |
| 99 |
} |
| 100 |
return |
| 101 |
(((srfScale(t[redPlane], pamP) >> 3) & 0x1f) << 11) | |
| 102 |
(((srfScale(t[grnPlane], pamP) >> 3) & 0x1f) << 6) | |
| 103 |
(((srfScale(t[bluPlane], pamP) >> 3) & 0x1f) << 0); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static uint8_t |
| 109 |
srfAlphaFromTuple(tuple const t, |
| 110 |
const struct pam * const pamP) { |
| 111 |
|
| 112 |
uint8_t retval; |
| 113 |
int haveOpacity; |
| 114 |
unsigned int opacityPlane; |
| 115 |
|
| 116 |
pnm_getopacity(pamP, &haveOpacity, &opacityPlane); |
| 117 |
|
| 118 |
if (haveOpacity) { |
| 119 |
uint8_t const scaled = srfScale(t[opacityPlane], pamP); |
| 120 |
|
| 121 |
retval = scaled == 0xff ? SRF_ALPHA_OPAQUE : 128 - (scaled >> 1); |
| 122 |
} else |
| 123 |
retval = SRF_ALPHA_OPAQUE; |
| 124 |
|
| 125 |
return retval; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static void |
| 131 |
convertRaster(struct pam * const pamP, |
| 132 |
struct srf_img * const imgP) { |
| 133 |
|
| 134 |
tuple * tuplerow; |
| 135 |
unsigned int row; |
| 136 |
|
| 137 |
tuplerow = pnm_allocpamrow(pamP); |
| 138 |
|
| 139 |
for (row = 0; row < pamP->height; ++row) { |
| 140 |
uint32_t const off = row * pamP->width; |
| 141 |
uint16_t * const data = &imgP->data.data[off]; |
| 142 |
unsigned char * const alpha = &imgP->alpha.data[off]; |
| 143 |
|
| 144 |
unsigned int col; |
| 145 |
|
| 146 |
pnm_readpamrow(pamP, tuplerow); |
| 147 |
|
| 148 |
for (col = 0; col < pamP->width; ++col) { |
| 149 |
alpha[col] = srfAlphaFromTuple(tuplerow[col], pamP); |
| 150 |
data[col] = srfColorFromTuple(tuplerow[col], pamP); |
| 151 |
} |
| 152 |
} |
| 153 |
pnm_freepamrow(tuplerow); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
static void |
| 159 |
convertImage(FILE * const ifP, |
| 160 |
struct srf * const srfP) { |
| 161 |
|
| 162 |
struct pam inpam; |
| 163 |
|
| 164 |
pnm_readpaminit(ifP, &inpam, PAM_STRUCT_SIZE(tuple_type)); |
| 165 |
|
| 166 |
if (verbose) |
| 167 |
pm_message("reading %ux%u image", inpam.width, inpam.height); |
| 168 |
|
| 169 |
srf_create_img(srfP, inpam.width, inpam.height); |
| 170 |
|
| 171 |
convertRaster(&inpam, &srfP->imgs[srfP->header.img_cnt-1]); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
int |
| 177 |
main(int argc, const char * argv[]) { |
| 178 |
|
| 179 |
struct cmdlineInfo cmdline; |
| 180 |
FILE * ifP; |
| 181 |
struct srf srf; |
| 182 |
int eof; /* No more images in input */ |
| 183 |
unsigned int imageSeq; |
| 184 |
/* Sequence of current image in input file. First = 0 */ |
| 185 |
|
| 186 |
pm_proginit(&argc, argv); |
| 187 |
|
| 188 |
parseCommandLine(argc, argv, &cmdline); |
| 189 |
|
| 190 |
verbose = cmdline.verbose; |
| 191 |
|
| 192 |
ifP = pm_openr(cmdline.inputFileName); |
| 193 |
|
| 194 |
srf_init(&srf); |
| 195 |
|
| 196 |
eof = FALSE; |
| 197 |
for (imageSeq = 0; !eof; ++imageSeq) { |
| 198 |
if (verbose) |
| 199 |
pm_message("Converting Image %u", imageSeq); |
| 200 |
|
| 201 |
convertImage(ifP, &srf); |
| 202 |
|
| 203 |
pnm_nextimage(ifP, &eof); |
| 204 |
} |
| 205 |
|
| 206 |
srf_write(stdout, &srf); |
| 207 |
|
| 208 |
srf_term(&srf); |
| 209 |
pm_closer(ifP); |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|