| 1 |
/* |
| 2 |
* Convert a ATARI Degas .pi3 file to a portable bitmap file. |
| 3 |
* |
| 4 |
* Author: David Beckemeyer |
| 5 |
* |
| 6 |
* This code was derived from the original gemtopbm program written |
| 7 |
* by Diomidis D. Spinellis. |
| 8 |
* |
| 9 |
* (C) Copyright 1988 David Beckemeyer and Diomidis D. Spinellis. |
| 10 |
* |
| 11 |
* Permission to use, copy, modify, and distribute this software and its |
| 12 |
* documentation for any purpose and without fee is hereby granted, |
| 13 |
* provided that the above copyright notice appear in all copies and that |
| 14 |
* both that copyright notice and this permission notice appear in |
| 15 |
* supporting documentation. |
| 16 |
* |
| 17 |
* This file is provided AS IS with no warranties of any kind. The author |
| 18 |
* shall have no liability with respect to the infringement of copyrights, |
| 19 |
* trade secrets or any patents by this file or any part thereof. In no |
| 20 |
* event will the author be liable for any lost revenue or profits or |
| 21 |
* other special, indirect and consequential damages. |
| 22 |
*/ |
| 23 |
|
| 24 |
#include <stdio.h> |
| 25 |
|
| 26 |
#include "pm_c_util.h" |
| 27 |
#include "mallocvar.h" |
| 28 |
#include "shhopt.h" |
| 29 |
#include "pbm.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
struct CmdlineInfo { |
| 34 |
/* All the information the user supplied in the command line, |
| 35 |
in a form easy for the program to use. |
| 36 |
*/ |
| 37 |
const char * inputFileName; /* Filename of input file */ |
| 38 |
unsigned int debug; |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
static void |
| 44 |
parseCommandLine(int argc, |
| 45 |
const char ** argv, |
| 46 |
struct CmdlineInfo * const cmdlineP) { |
| 47 |
/* -------------------------------------------------------------------------- |
| 48 |
Parse program command line described in Unix standard form by argc |
| 49 |
and argv. Return the information in the options as *cmdlineP. |
| 50 |
|
| 51 |
If command line is internally inconsistent (invalid options, etc.), |
| 52 |
issue error message to stderr and abort program. |
| 53 |
|
| 54 |
Note that the strings we return are stored in the storage that |
| 55 |
was passed to us as the argv array. We also trash *argv. |
| 56 |
--------------------------------------------------------------------------*/ |
| 57 |
optEntry * option_def; |
| 58 |
optStruct3 opt; |
| 59 |
unsigned int option_def_index; |
| 60 |
|
| 61 |
MALLOCARRAY_NOFAIL(option_def, 100); |
| 62 |
|
| 63 |
option_def_index = 0; /* incremented by OPTENT3 */ |
| 64 |
OPTENT3(0, "debug", OPT_FLAG, NULL, &cmdlineP->debug, 0); |
| 65 |
|
| 66 |
opt.opt_table = option_def; |
| 67 |
opt.short_allowed = FALSE; /* We have no short (old-fashioned) options */ |
| 68 |
opt.allowNegNum = FALSE; /* We have no parms that are negative numbers */ |
| 69 |
|
| 70 |
pm_optParseOptions4(&argc, argv, opt, sizeof(opt), 0); |
| 71 |
/* Uses and sets argc, argv, and some of *cmdlineP and others. */ |
| 72 |
|
| 73 |
if (argc-1 < 1) |
| 74 |
cmdlineP->inputFileName = "-"; |
| 75 |
else { |
| 76 |
cmdlineP->inputFileName = argv[1]; |
| 77 |
|
| 78 |
if (argc-1 > 1) |
| 79 |
pm_error("Program takes zero or one argument (filename). You " |
| 80 |
"specified %u", argc-1); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static void |
| 87 |
readAndValidateHeader(FILE * const ifP, |
| 88 |
bool const debug, |
| 89 |
bool * const reverseP) { |
| 90 |
|
| 91 |
short item; |
| 92 |
|
| 93 |
pm_readbigshort(ifP, &item); |
| 94 |
|
| 95 |
if (debug) |
| 96 |
pm_message("resolution is %d", item); |
| 97 |
|
| 98 |
/* only handles hi-rez 640x400 */ |
| 99 |
if (item != 2) |
| 100 |
pm_error("bad resolution %d", item); |
| 101 |
|
| 102 |
pm_readbigshort(ifP, &item); |
| 103 |
|
| 104 |
*reverseP = (item == 0); |
| 105 |
|
| 106 |
{ |
| 107 |
unsigned int i; |
| 108 |
|
| 109 |
for (i = 1; i < 16; ++i) |
| 110 |
pm_readbigshort (ifP, &item); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
int |
| 117 |
main(int argc, const char ** argv) { |
| 118 |
|
| 119 |
unsigned int const rows = 400; |
| 120 |
unsigned int const cols = 640; |
| 121 |
|
| 122 |
struct CmdlineInfo cmdline; |
| 123 |
FILE * ifP; |
| 124 |
unsigned int row; |
| 125 |
bit * bitrow; |
| 126 |
bool reverse; |
| 127 |
|
| 128 |
pm_proginit(&argc, argv); |
| 129 |
|
| 130 |
parseCommandLine(argc, argv, &cmdline); |
| 131 |
|
| 132 |
ifP = pm_openr(cmdline.inputFileName); |
| 133 |
|
| 134 |
readAndValidateHeader(ifP, cmdline.debug, &reverse); |
| 135 |
|
| 136 |
pbm_writepbminit(stdout, cols, rows, 0); |
| 137 |
|
| 138 |
bitrow = pbm_allocrow_packed(cols); |
| 139 |
|
| 140 |
for (row = 0; row < rows; ++row) { |
| 141 |
unsigned int const colChars = cols / 8; |
| 142 |
|
| 143 |
unsigned int bytesReadCt; |
| 144 |
|
| 145 |
bytesReadCt = fread(bitrow, cols / 8, 1, ifP); |
| 146 |
if (bytesReadCt != 1) { |
| 147 |
if (feof(ifP)) |
| 148 |
pm_error( "EOF reached while reading image data" ); |
| 149 |
else |
| 150 |
pm_error("read error while reading image data"); |
| 151 |
} |
| 152 |
|
| 153 |
if (reverse) { |
| 154 |
/* flip all pixels */ |
| 155 |
unsigned int colChar; |
| 156 |
for (colChar = 0; colChar < colChars; ++colChar) |
| 157 |
bitrow[colChar] = ~bitrow[colChar]; |
| 158 |
} |
| 159 |
pbm_writepbmrow_packed(stdout, bitrow, cols, 0); |
| 160 |
} |
| 161 |
|
| 162 |
pbm_freerow_packed(bitrow); |
| 163 |
pm_close(ifP); |
| 164 |
pm_close(stdout); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|