| 1 |
/* |
| 2 |
* cistopbm: Convert images in the CompuServe RLE format to PBM |
| 3 |
* Copyright (C) 2009 John Elliott |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program; if not, write to the Free Software |
| 17 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <stdio.h> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include "pbm.h" |
| 25 |
|
| 26 |
/* The maximum length of a run. Limit it to 0x5E bytes so that it is always |
| 27 |
* represented by a printable character 0x20-0x7E */ |
| 28 |
#define MAXRUNLENGTH 0x5E |
| 29 |
|
| 30 |
static void syntax(const char *prog) |
| 31 |
{ |
| 32 |
pm_usage(" { options } { input } }\n\n" |
| 33 |
"Input file should be in PBM format.\n" |
| 34 |
"Output will be in CompuServe RLE format.\n" |
| 35 |
"Options:\n" |
| 36 |
"-i, --inverse: Reverse black and white.\n" |
| 37 |
"-w, --whitebg: White background.\n" |
| 38 |
"--: End of options\n\n" |
| 39 |
"pbmtocis v1.00, Copyright 2009 John Elliott <[email protected]>\n" |
| 40 |
"This program is redistributable under the terms of the GNU General Public\n" |
| 41 |
"License, version 2 or later.\n" |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
int main(int argc, const char **argv) |
| 48 |
{ |
| 49 |
FILE *ofP = stdout; |
| 50 |
FILE *ifP; |
| 51 |
int inoptions = 1; |
| 52 |
int n, x, y; |
| 53 |
int bg = PBM_BLACK; /* Default colouring is white on black */ |
| 54 |
int inverse = 0; |
| 55 |
int cell, last, run; |
| 56 |
const char *inpname = NULL; |
| 57 |
|
| 58 |
int outh, outw; |
| 59 |
int height, width; |
| 60 |
bit **bits; |
| 61 |
|
| 62 |
pm_proginit(&argc, argv); |
| 63 |
|
| 64 |
for (n = 1; n < argc; n++) |
| 65 |
{ |
| 66 |
if (!strcmp(argv[n], "--")) |
| 67 |
{ |
| 68 |
inoptions = 0; |
| 69 |
continue; |
| 70 |
} |
| 71 |
if (inoptions) |
| 72 |
{ |
| 73 |
if (pm_keymatch(argv[n], "-h", 2) || |
| 74 |
pm_keymatch(argv[n], "-H", 2) || |
| 75 |
pm_keymatch(argv[n], "--help", 6)) |
| 76 |
{ |
| 77 |
syntax(argv[0]); |
| 78 |
return EXIT_SUCCESS; |
| 79 |
} |
| 80 |
if (pm_keymatch(argv[n], "-i", 2) || |
| 81 |
pm_keymatch(argv[n], "-I", 2) || |
| 82 |
pm_keymatch(argv[n], "--inverse", 9)) |
| 83 |
{ |
| 84 |
inverse = 1; |
| 85 |
continue; |
| 86 |
} |
| 87 |
if (pm_keymatch(argv[n], "-w", 2) || |
| 88 |
pm_keymatch(argv[n], "-W", 2) || |
| 89 |
pm_keymatch(argv[n], "--whitebg", 9)) |
| 90 |
{ |
| 91 |
bg = PBM_WHITE; |
| 92 |
continue; |
| 93 |
} |
| 94 |
if (argv[n][0] == '-' && argv[n][1] != 0) |
| 95 |
{ |
| 96 |
pm_message("Unknown option: %s", argv[n]); |
| 97 |
syntax(argv[0]); |
| 98 |
return EXIT_FAILURE; |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (inpname == NULL) inpname = argv[n]; |
| 103 |
else { syntax(argv[0]); return EXIT_FAILURE; } |
| 104 |
} |
| 105 |
if (inpname == NULL) inpname = "-"; |
| 106 |
ifP = pm_openr(inpname); |
| 107 |
|
| 108 |
/* Load the PBM */ |
| 109 |
bits = pbm_readpbm(ifP, &width, &height); |
| 110 |
|
| 111 |
if (width <= 128 && height <= 96) { outw = 128; outh = 96; } |
| 112 |
else if (width <= 256 && height <= 192) { outw = 256; outh = 192; } |
| 113 |
else |
| 114 |
{ |
| 115 |
outw = 256; |
| 116 |
outh = 192; |
| 117 |
pm_message("Warning: Input file is larger than 256x192. " |
| 118 |
"It will be cropped."); |
| 119 |
} |
| 120 |
/* Write the magic number */ |
| 121 |
fputc(0x1B, ofP); |
| 122 |
fputc(0x47, ofP); |
| 123 |
fputc((outw == 128) ? 0x4D : 0x48, ofP); |
| 124 |
|
| 125 |
/* And now start encoding */ |
| 126 |
y = x = 0; |
| 127 |
last = PBM_BLACK; |
| 128 |
run = 0; |
| 129 |
while (y < outh) |
| 130 |
{ |
| 131 |
if (x < width && y < height) |
| 132 |
{ |
| 133 |
cell = bits[y][x]; |
| 134 |
if (inverse) cell ^= (PBM_BLACK ^ PBM_WHITE); |
| 135 |
} |
| 136 |
else cell = bg; |
| 137 |
|
| 138 |
if (cell == last) /* Cell is part of current run */ |
| 139 |
{ |
| 140 |
++run; |
| 141 |
if (run > MAXRUNLENGTH) |
| 142 |
{ |
| 143 |
fputc(0x20 + MAXRUNLENGTH, ofP); |
| 144 |
fputc(0x20, ofP); |
| 145 |
run -= MAXRUNLENGTH; |
| 146 |
} |
| 147 |
} |
| 148 |
else /* change */ |
| 149 |
{ |
| 150 |
fputc(run + 0x20, ofP); |
| 151 |
last = last ^ (PBM_BLACK ^ PBM_WHITE); |
| 152 |
run = 1; |
| 153 |
} |
| 154 |
++x; |
| 155 |
if (x >= outw) { x = 0; ++y; } |
| 156 |
} |
| 157 |
if (last == bg) /* Last cell written was background. Write foreground */ |
| 158 |
{ |
| 159 |
fputc(run + 0x20, ofP); |
| 160 |
} |
| 161 |
else if (run) /* Write background and foreground */ |
| 162 |
{ |
| 163 |
fputc(run + 0x20, ofP); |
| 164 |
fputc(0x20, ofP); |
| 165 |
} |
| 166 |
/* Write the end-graphics signature */ |
| 167 |
fputc(0x1B, ofP); |
| 168 |
fputc(0x47, ofP); |
| 169 |
fputc(0x4E, ofP); |
| 170 |
pm_close(ifP); |
| 171 |
return 0; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|