| 1 |
/* ddbugtopbm - convert Diddle/DiddleBug sketch DB to PBM files. |
| 2 |
* Copyright (c) 2002 Russell Marks. See COPYING for licence details. |
| 3 |
* |
| 4 |
* The decompression code (uncompress_sketch() is directly from |
| 5 |
* DiddleBug itself, which like ddbugtopbm is distributed under the |
| 6 |
* terms of the GNU GPL. As such, the following copyright applies to |
| 7 |
* uncompress_sketch(): |
| 8 |
* |
| 9 |
* Copyright (c) 1999,2000 Mitch Blevins <[email protected]>. |
| 10 |
* |
| 11 |
* |
| 12 |
* This program is free software; you can redistribute it and/or modify |
| 13 |
* it under the terms of the GNU General Public License as published by |
| 14 |
* the Free Software Foundation; either version 2 of the License, or (at |
| 15 |
* your option) any later version. |
| 16 |
* |
| 17 |
* This program is distributed in the hope that it will be useful, but |
| 18 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 |
* General Public License for more details. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU General Public License |
| 23 |
* along with this program; if not, write to the Free Software |
| 24 |
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 |
* |
| 26 |
* Adapted to Netpbm by Bryan Henderson 2003.08.09. Bryan got his copy |
| 27 |
* from ftp:ibiblio.com/pub/linux/apps/graphics/convert, dated 2002.08.21. |
| 28 |
*/ |
| 29 |
|
| 30 |
#include <stdio.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <string.h> |
| 33 |
#include <ctype.h> |
| 34 |
|
| 35 |
#include "pm_c_util.h" |
| 36 |
#include "pbm.h" |
| 37 |
#include "mallocvar.h" |
| 38 |
|
| 39 |
/* this is basically UncompressSketch() from DiddleBug 2.50's diddlebug.c */ |
| 40 |
static void |
| 41 |
uncompress_sketch(unsigned char * const cPtr, |
| 42 |
unsigned char * const uPtr, |
| 43 |
int const size) { |
| 44 |
int i, j; |
| 45 |
unsigned char num_bytes; |
| 46 |
|
| 47 |
j = 0; |
| 48 |
for (i=0; i<size; ++i) { |
| 49 |
if (cPtr[i]&0x80) { |
| 50 |
num_bytes = cPtr[i]&0x3f; /* ~(0x40|0x80) */ |
| 51 |
++num_bytes; |
| 52 |
if (cPtr[i]&0x40) { |
| 53 |
/* Mixed */ |
| 54 |
memmove(uPtr+j, cPtr+i+1, num_bytes); |
| 55 |
i += num_bytes; |
| 56 |
} else { |
| 57 |
/* Black */ |
| 58 |
memset(uPtr+j, 0xff, num_bytes); |
| 59 |
} |
| 60 |
} else { |
| 61 |
/* White */ |
| 62 |
num_bytes = cPtr[i]&0x7f; /* ~0x80 */ |
| 63 |
++num_bytes; |
| 64 |
memset(uPtr+j, 0x00, num_bytes); |
| 65 |
} |
| 66 |
j += num_bytes; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
static const char * |
| 73 |
make_noname(void) { |
| 74 |
static char name[128]; |
| 75 |
static int num=0; |
| 76 |
FILE *out; |
| 77 |
|
| 78 |
out = NULL; |
| 79 |
|
| 80 |
do { |
| 81 |
num++; |
| 82 |
if (out != NULL) |
| 83 |
fclose(out); |
| 84 |
sprintf(name, "sketch-%04d.pbm", num); |
| 85 |
} while (num<10000 && (out = fopen(name, "rb")) != NULL); |
| 86 |
|
| 87 |
if (num>=10000) |
| 88 |
pm_error("too many unnamed sketches!"); |
| 89 |
|
| 90 |
return(name); |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
int |
| 96 |
main(int argc, char ** argv) { |
| 97 |
FILE * const in=stdin; |
| 98 |
|
| 99 |
static unsigned char buf[64*1024]; |
| 100 |
int *recoffsets; |
| 101 |
int f; |
| 102 |
int numrecs; |
| 103 |
bool is_diddle; |
| 104 |
|
| 105 |
pbm_init(&argc, argv); |
| 106 |
|
| 107 |
if (argc-1 > 0) |
| 108 |
pm_error("Program takes no arguments."); |
| 109 |
|
| 110 |
/* main DB header */ |
| 111 |
fread(buf, 1, 64, in); |
| 112 |
|
| 113 |
fread(buf, 1, 14, in); |
| 114 |
if (memcmp(buf, "DIDL", 4) != 0 && memcmp(buf, "DIDB", 4) !=0 ) |
| 115 |
pm_error("not a Diddle or DiddleBug DB."); |
| 116 |
is_diddle = (memcmp(buf, "DIDL", 4) == 0); |
| 117 |
numrecs = buf[12]*256+buf[13]; |
| 118 |
|
| 119 |
MALLOCARRAY_NOFAIL(recoffsets, numrecs); |
| 120 |
|
| 121 |
for (f = 0; f < numrecs; ++f) { |
| 122 |
fread(buf, 1, 8, in); |
| 123 |
recoffsets[f] = ((buf[0]<<24)|(buf[1]<<16)|(buf[2]<<8)|buf[3]); |
| 124 |
} |
| 125 |
|
| 126 |
for (f = 0; f < numrecs; ++f) { |
| 127 |
static unsigned char obuf[64*1024]; |
| 128 |
char * nameptr; |
| 129 |
char * ptr; |
| 130 |
const char * outfilename; |
| 131 |
FILE *out; |
| 132 |
|
| 133 |
fseek(in, recoffsets[f], SEEK_SET); |
| 134 |
fread(buf, 1, 16*1024, in); /* XXX crappy! */ |
| 135 |
|
| 136 |
if (is_diddle) { |
| 137 |
/* Diddle */ |
| 138 |
memcpy(obuf, buf, 160*160/8); |
| 139 |
nameptr=(char*)(buf+(160*160/8)); |
| 140 |
} else { |
| 141 |
/* DiddleBug */ |
| 142 |
int const sketchlen = buf[8]*256+buf[9]; |
| 143 |
uncompress_sketch(buf+26+80+1, obuf, sketchlen-80-1); |
| 144 |
nameptr = (char*)(buf+26+sketchlen); |
| 145 |
} |
| 146 |
|
| 147 |
for (ptr = nameptr; *ptr; ++ptr) { |
| 148 |
if (!isalnum(*ptr) && strchr("()-_+=[]:;,.<>?",*ptr) == NULL) |
| 149 |
*ptr='_'; |
| 150 |
if (isupper(*ptr)) |
| 151 |
*ptr = tolower(*ptr); |
| 152 |
} |
| 153 |
|
| 154 |
if (*nameptr==0) |
| 155 |
outfilename = make_noname(); |
| 156 |
else { |
| 157 |
strcat(nameptr, ".pbm"); |
| 158 |
outfilename = nameptr; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
pm_message("extracting sketch %2d as `%s'", f, outfilename); |
| 163 |
if((out=fopen(outfilename,"wb"))==NULL) |
| 164 |
pm_message("WARNING: couldn't open file '%s'. Carrying on...", |
| 165 |
outfilename); |
| 166 |
else { |
| 167 |
pbm_writepbminit(out, 160, 160, FALSE); |
| 168 |
fwrite(obuf,1,160*160/8,out); |
| 169 |
fclose(out); |
| 170 |
} |
| 171 |
} |
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|