| 1 |
/* |
| 2 |
* Convert a SRF (Garmin vehicle) to a PAM image |
| 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 <assert.h> |
| 15 |
#include <stdio.h> |
| 16 |
|
| 17 |
#include "pm_c_util.h" |
| 18 |
#include "shhopt.h" |
| 19 |
#include "mallocvar.h" |
| 20 |
#include "nstring.h" |
| 21 |
#include "pam.h" |
| 22 |
#include "srf.h" |
| 23 |
|
| 24 |
struct cmdlineInfo { |
| 25 |
/* All the information the user supplied in the command line, |
| 26 |
in a form easy for the program to use. |
| 27 |
*/ |
| 28 |
const char * inputFileName; /* '-' if stdin */ |
| 29 |
unsigned int verbose; |
| 30 |
}; |
| 31 |
|
| 32 |
static bool verbose; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
static void |
| 37 |
parseCommandLine(int argc, const char ** argv, |
| 38 |
struct cmdlineInfo * const cmdlineP) { |
| 39 |
/*---------------------------------------------------------------------------- |
| 40 |
parse program command line described in Unix standard form by argc |
| 41 |
and argv. Return the information in the options as *cmdlineP. |
| 42 |
|
| 43 |
If command line is internally inconsistent (invalid options, etc.), |
| 44 |
issue error message to stderr and abort program. |
| 45 |
|
| 46 |
Note that the strings we return are stored in the storage that |
| 47 |
was passed to us as the argv array. We also trash *argv. |
| 48 |
-----------------------------------------------------------------------------*/ |
| 49 |
optEntry * option_def; /* Used by OPTENT3 */ |
| 50 |
optStruct3 opt; |
| 51 |
|
| 52 |
unsigned int option_def_index; |
| 53 |
|
| 54 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 55 |
|
| 56 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 57 |
OPTENT3(0, "verbose", OPT_FLAG, NULL, |
| 58 |
&cmdlineP->verbose, 0); |
| 59 |
|
| 60 |
opt.opt_table = option_def; |
| 61 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 62 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 63 |
|
| 64 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 65 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 66 |
|
| 67 |
if (argc-1 < 1) |
| 68 |
cmdlineP->inputFileName = "-"; |
| 69 |
else if (argc-1 == 1) |
| 70 |
cmdlineP->inputFileName = argv[1]; |
| 71 |
else |
| 72 |
pm_error("Program takes at most one argument: input file name"); |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
static unsigned int |
| 78 |
srfRed(uint16_t const pixel) { |
| 79 |
return ((pixel >> 11) & 0x1f) << 3; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
static unsigned int |
| 85 |
srfGrn(uint16_t const pixel) { |
| 86 |
return ((pixel >> 6) & 0x1f) << 3; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
static unsigned int |
| 92 |
srfBlu(uint16_t const pixel) { |
| 93 |
return ((pixel >> 0) & 0x1f) << 3; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
static uint8_t |
| 99 |
srfAlpha(uint8_t const d) { |
| 100 |
|
| 101 |
uint8_t retval; |
| 102 |
|
| 103 |
if (d == SRF_ALPHA_OPAQUE) |
| 104 |
retval = 0xff; |
| 105 |
else |
| 106 |
retval = (128 - d) << 1; |
| 107 |
|
| 108 |
return retval; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
static void |
| 114 |
writeRaster(struct pam * const pamP, |
| 115 |
struct srf_img * const imgP) { |
| 116 |
|
| 117 |
tuple * tuplerow; |
| 118 |
unsigned int row; |
| 119 |
|
| 120 |
assert(imgP->header.width <= pamP->width); |
| 121 |
|
| 122 |
tuplerow = pnm_allocpamrow(pamP); |
| 123 |
|
| 124 |
for (row = 0; row < imgP->header.height; ++row) { |
| 125 |
unsigned int const rowStart = row * imgP->header.width; |
| 126 |
|
| 127 |
unsigned int col; |
| 128 |
|
| 129 |
for (col = 0; col < imgP->header.width; ++col) { |
| 130 |
uint16_t const data = imgP->data.data[rowStart + col]; |
| 131 |
uint16_t const alpha = imgP->alpha.data[rowStart + col]; |
| 132 |
|
| 133 |
assert(col < pamP->width); |
| 134 |
|
| 135 |
tuplerow[col][PAM_RED_PLANE] = srfRed(data); |
| 136 |
tuplerow[col][PAM_GRN_PLANE] = srfGrn(data); |
| 137 |
tuplerow[col][PAM_BLU_PLANE] = srfBlu(data); |
| 138 |
tuplerow[col][PAM_TRN_PLANE] = srfAlpha(alpha); |
| 139 |
} |
| 140 |
|
| 141 |
for (; col < pamP->width; ++col) { |
| 142 |
tuplerow[col][PAM_RED_PLANE] = 0; |
| 143 |
tuplerow[col][PAM_GRN_PLANE] = 0; |
| 144 |
tuplerow[col][PAM_BLU_PLANE] = 0; |
| 145 |
tuplerow[col][PAM_TRN_PLANE] = 0; |
| 146 |
} |
| 147 |
|
| 148 |
pnm_writepamrow(pamP, tuplerow); |
| 149 |
} |
| 150 |
pnm_freepamrow(tuplerow); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
static void |
| 156 |
convertOneImage(struct srf_img * const imgP, |
| 157 |
FILE * const ofP) { |
| 158 |
|
| 159 |
const char * comment = "Produced by srftopam"; /* constant */ |
| 160 |
|
| 161 |
struct pam outPam; |
| 162 |
|
| 163 |
outPam.size = sizeof(struct pam); |
| 164 |
outPam.len = PAM_STRUCT_SIZE(comment_p); |
| 165 |
outPam.file = ofP; |
| 166 |
outPam.format = PAM_FORMAT; |
| 167 |
outPam.plainformat = 0; |
| 168 |
outPam.width = imgP->header.width; |
| 169 |
outPam.height = imgP->header.height; |
| 170 |
outPam.depth = 4; |
| 171 |
outPam.maxval = 255; |
| 172 |
outPam.bytes_per_sample = 1; |
| 173 |
sprintf(outPam.tuple_type, "RGB_ALPHA"); |
| 174 |
outPam.allocation_depth = 4; |
| 175 |
outPam.comment_p = &comment; |
| 176 |
|
| 177 |
pnm_writepaminit(&outPam); |
| 178 |
|
| 179 |
writeRaster(&outPam, imgP); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
static void |
| 185 |
srftopam(struct cmdlineInfo const cmdline, |
| 186 |
FILE * const ifP, |
| 187 |
FILE * const ofP) { |
| 188 |
|
| 189 |
unsigned int imgSeq; |
| 190 |
struct srf srf; |
| 191 |
|
| 192 |
srf_read(ifP, verbose, &srf); |
| 193 |
|
| 194 |
for (imgSeq = 0; imgSeq < srf.header.img_cnt; ++imgSeq) { |
| 195 |
if (verbose) |
| 196 |
pm_message("Converting Image %u", imgSeq); |
| 197 |
convertOneImage(&srf.imgs[imgSeq], ofP); |
| 198 |
} |
| 199 |
|
| 200 |
srf_term(&srf); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
int |
| 206 |
main(int argc, const char * argv[]) { |
| 207 |
|
| 208 |
struct cmdlineInfo cmdline; |
| 209 |
FILE * ifP; |
| 210 |
|
| 211 |
pm_proginit(&argc, argv); |
| 212 |
|
| 213 |
parseCommandLine(argc, argv, &cmdline); |
| 214 |
|
| 215 |
verbose = cmdline.verbose; |
| 216 |
|
| 217 |
ifP = pm_openr(cmdline.inputFileName); |
| 218 |
|
| 219 |
srftopam(cmdline, ifP, stdout); |
| 220 |
|
| 221 |
pm_closer(ifP); |
| 222 |
|
| 223 |
return 0; |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
|