/* diddlebug-db * Copyright (C) 2000 Kengo Ichiki * $Id: diddlebug-db.c,v 1.2 2000/09/05 16:05:04 ichiki Exp $ * * based on doodle-db.c * Copyright 1997, Roger E Critchlow Jr., San Francisco, California. * ** doodle-db - maintain a doodle database file under unix using ** the pilot-link libraries and the pbm package. ** ** The doodle database consists of an indefinite number of 160x160 ** pixel images, one to each record. ** ** Two operations are implemented: ** ** -x extract pbm format files from a database ** -c create a database and append pbm files onto it ** ** From these you can synthesize most of what anyone does to an ** archive file. */ #include #include #include #include #include #include "pi-file.h" #include "doodle.h" char *make_pbm_file_name(char *db_file_name, int record) { static int n = 0; static char *pbm = NULL; if (n == 0 || strlen(db_file_name)+32 > n) { if ((pbm = malloc(n = strlen(db_file_name)+32)) == NULL) pm_error("failed to allocate name buffer for %s\n", db_file_name); } strcpy(pbm, db_file_name); if (strrchr(pbm, '.') != NULL) sprintf(strrchr(pbm, '.'), "%d.pbm", record+1); else sprintf(pbm+strlen(pbm), "%d.pbm", record+1); return pbm; } char *make_database_name(char *db_file_name) { static char *db = NULL; char *p1, *p2; int n; if ((p1 = strrchr(db_file_name, '/')) == NULL) p1 = db_file_name; if ((p2 = strrchr(db_file_name, '.')) == NULL) p2 = db_file_name+strlen(db_file_name); n = p2-p1; if (db == NULL) db = malloc(n+1); else db = realloc(db, n+1); if (db == NULL) pm_error("failed to allocate name buffer for %s\n", db_file_name); strncpy(db, "", n+1); strncpy(db, p1, n); return db; } /* ** UncompressSketch - assumes uPtr of 160x160 bits * * based on diddlebug.c ** diddlebug - a yellow-sticky-note-thingy ** Copyright (c) 1999, Mitch Blevins . ** Licensed under the GNU GPL, version 2 or later ** See file "COPYING" that you should have received with this program ** or visit http://www.gnu.org/copyleft/gpl.html ** ** DiddleBug is based on Diddle, which is based on Doodle ** Doodle is Copyright 1997, Roger E Critchlow Jr., San Francisco, California. ** All rights reserved. Fair use permitted. Caveat emptor. No warranty. */ void UncompressSketch (char *cPtr, char *uPtr, int size) { unsigned int i, j, k; unsigned char num_bytes; j = 0; for (i = 0; i < size; i ++) { if (cPtr [i] & 0x80) { num_bytes = cPtr [i] & 0x3f; /* ~(0x40|0x80) */ num_bytes++; if (num_bytes > (3200 - j)) break; if (cPtr [i] & 0x40) { /* Mixed */ /*MemMove (uPtr+j, cPtr+i+1, num_bytes);*/ for (k = 0; k < num_bytes; k ++) uPtr [j + k] = cPtr [i + 1 + k]; i += num_bytes; } else { /* Black */ /*MemSet(uPtr+j, num_bytes, 0xff);*/ for (k = 0; k < num_bytes; k ++) uPtr [j + k] = 0xff; } } else { /* White */ num_bytes = cPtr [i] & 0x7f; /* ~0x80 */ num_bytes ++; if (num_bytes > (3200-j)) break; /*MemSet(uPtr+j, num_bytes, 0x00);*/ for (k = 0; k < num_bytes; k ++) uPtr [j + k] = 0; } j += num_bytes; } } int main(int argc, char *argv[]) { /* Initialize pbm package */ pbm_init(&argc, argv); /* Test for adequate arguments */ if (argc >= 3) { /* Extract doodle database */ if (strcmp(argv[1], "-x") == 0) { struct pi_file *db; int i, j, k, size, attr, cat; int offset = 26 + 80 + 1; void * cbufp; char * bufp; pi_uid_t uid; FILE *pbm; bit **bits = pbm_allocarray(160, 160); /* Open Pilot database */ if ((db = pi_file_open(argv[2])) == NULL) pm_error("could not open DoodleDB: %s\n", argv[2]); for (i = 0; ; i ++) { /* Read database record */ if (pi_file_read_record(db, i, &cbufp, &size, &attr, &cat, &uid) != 0) break; size -= (offset + 2); /* i don't know why '+2' (k.i.)*/ /* uncompress */ bufp = malloc (160 * 160 / 8); if (bufp == NULL) pm_error("malloc failed\n"); UncompressSketch (cbufp + offset, bufp, size); /* Open pbm file */ pbm = pm_openw(make_pbm_file_name(argv[2], i)); /* Translate database record to bits */ for (j = 0; j < 160; j += 1) for (k = 0; k < 160; k += 1) bits[j][k] = ((char *)bufp)[(j*160+k)/8] & (0x80>>((j*160+k)%8)) ? PBM_BLACK : PBM_WHITE; /* Write pbm file */ pbm_writepbm(pbm, bits, 160, 160, 0); /* Close the pbm file */ pm_close(pbm); if (bufp) { free (bufp); bufp = NULL; } } /* Free the bits */ pbm_freearray(bits, 160); /* Close the database */ pi_file_close(db); /* Done */ return 0; } /* Create doodle database */ if (strcmp(argv[1], "-c") == 0) { fprintf (stderr, "create is not supported yet... sorry.\n"); } } fprintf(stderr, "usage: %s -x DoodleDB.pdb\n" "or: %s -c DoodleDB.pdb [file.pbm ...]\n", argv[0], argv[0]); return 1; }