(Message archief/1998/verstuurd:568)
To: redhat@redhat.com
cc: bug@redhat.com, djb@redhat.com, neal@ctd.comsat.com
Reply-To: janneke@gnu.org
Subject: [Resend] patch to libgr-2.0.13
Date: Tue, 20 Oct 1998 22:31:33 +0300
From: Jan Nieuwenhuizen <jantien@appel>

[Resend]
I've sent this mail twice before to djb, so now i'm resending it to redhat
and bug.

Hello Donnie,

The program pktopbm of my libgr-2.0.13 was seriously broken so i
fixed it,  but I doubt if the package is still (actively) being
maintained.  Because it seems you made the latests updates to my 
libgr i'm sending you the patch too.

Greetings,

Jan.

ps: as an aside, has RedHat considered distributing LilyPond?

Jan Nieuwenhuizen <janneke@gnu.org> | GNU LilyPond - The music typesetter
http://www.xs4all.nl/~jantien       | http://www.xs4all.nl/~jantien/lilypond

diff -urN ../libgr-2.0.13/ChangeLog ./ChangeLog
--- ../libgr-2.0.13/ChangeLog	Wed Oct 30 05:55:28 1996
+++ ./ChangeLog	Tue Sep 22 15:37:36 1998
@@ -1,3 +1,7 @@
+Tue Sep 22 15:34:12 1998    <janneke@gnu.org>
+
+	* fixed pktopbm
+
 Tue Oct 29 23:33:18 1996    <neal@ctd.comsat.com>
 
 	* libgr-2.0.11 released.
diff -urN ../libgr-2.0.13/pbm/pktopbm.c ./pbm/pktopbm.c
--- ../libgr-2.0.13/pbm/pktopbm.c	Sat Apr  5 15:23:08 1997
+++ ./pbm/pktopbm.c	Tue Sep 22 16:50:14 1998
@@ -1,5 +1,12 @@
 /*
   pktopbm, adapted from "pktopx in C by Tomas Rokicki" by AJCD 1/8/90
+  1998-09-22: jcn <janneke@gnu.org>
+     - lots of bugfixes:
+	 * always read x/y offset bytes (3x)
+	 * reset bmx, bmy to defaults for each char
+	 * fix bitmap y placement of dynamically packed char
+	 * skip char early if no output file allocated
+     - added debug output
   
   compile with: cc -lpbm -o pktopbm pktopbm.c
   */
@@ -26,6 +33,9 @@
 static eightbits bitweight ;
 static integer repeatcount ;
 static integer flagbyte ;
+static integer debug=0;
+
+#define dprintf(s,d) if (debug) printf(s,d)
 
 /* add a suffix to a filename in an allocated space */
 static void
@@ -165,18 +175,20 @@
      int argc ;
      char *argv[] ;
 {
+	integer x;
    integer endofpacket ;
    boolean turnon ;
    integer i, j;
    integer car ;
    integer bmx=0, bmy=0;
+   integer set_bmx=0, set_bmy=0;
    bit row[MAXROWWIDTH+1] ;
-   char *usage = "pkfile[.pk] [[-x width] [-y height] [-c num] pbmfile]...";
+   char *usage = "pkfile[.pk] [-d] [[-x width] [-y height] [-c num] pbmfile]...";
    
    pbm_init(&argc, argv);
    for (i = 0 ; i < MAXPKCHAR ; i ++) filename[i] = NULL ;
 
-   pm_message("This is PKtoPBM, version 2.4") ;
+   pm_message("This is PKtoPBM, version 2.5") ;
 
    if (--argc < 1) pm_usage(usage) ;
 
@@ -184,19 +196,20 @@
    pktopbm_add_suffix(pkname, ".pk") ;
 
    car = 0 ;
+   /* urg: use getopt */
    while (++argv, --argc) {
       if (argv[0][0] == '-' && argv[0][1])
 	 switch (argv[0][1]) {
        case 'X':
        case 'x':
 	  if (argv[0][2]) bmx = atoi(*argv+2) ;
-	  else if (++argv, --argc) bmx = atoi(*argv) ;
+	  else if (++argv, --argc) set_bmx = atoi(*argv) ;
 	  else pm_usage(usage) ;
 	  continue ;
        case 'Y':
        case 'y':
 	  if (argv[0][2]) bmy = atoi(*argv+2) ;
-	  else if (++argv, --argc) bmy = atoi(*argv) ;
+	  else if (++argv, --argc) set_bmy = atoi(*argv) ;
 	  else pm_usage(usage) ;
 	  continue ;
 	 case 'C':
@@ -205,6 +218,9 @@
 	    else if (++argv, --argc) car = atoi(*argv) ;
 	    else pm_usage(usage) ;
 	    break ;
+	 case 'd':
+	    debug=1;
+	    break ;
 	 default:
 	    pm_usage(usage) ;
 	 } else if (car < 0 || car >= MAXPKCHAR) {
@@ -229,6 +245,8 @@
       integer xoffs=0, yoffs=0;
       FILE *ofp;
 
+      bmx=set_bmx;
+      bmy=set_bmy;
       dynf = (flagbyte>>4) ;			/* get dynamic packing value */
       flagbyte &= 15 ;
       turnon = (flagbyte >= 8) ;		/* black or white initially? */
@@ -237,61 +255,82 @@
 	 integer packetlength = get32() ;	/* character packet length */
 	 car = get32() ;			/* character number */
 	 endofpacket = packetlength + pktopbm_pkloc ;	/* calculate end of packet */
-	 if (car >= MAXPKCHAR || car < 0) {
+         if ((car >= MAXPKCHAR) || !filename[car]) {
 	    ignorechar(car, endofpacket);
 	    continue;
 	 }
+	 dprintf ("flagbyte7\n", 0);
+	 dprintf ("car: %d\n", car);
 	 get32() ; 				/* ignore tfmwidth */
-	 get32() ;				/* ignore horiz escapement */
-	 get32() ;				/* ignore vert escapement */
+	 x=get32() ;				/* ignore horiz escapement */
+	 x=get32() ;				/* ignore vert escapement */
+	 dprintf ("horiz esc %d\n", x);
+	 dprintf ("vert esc %d\n", x);
 	 cwidth = get32() ;			/* bounding box width */
 	 cheight = get32() ;			/* bounding box height */
+	 dprintf ("cwidth %d\n", cwidth);
+	 dprintf ("cheight %d\n", cheight);
 	 if (cwidth < 0 || cheight < 0 || cwidth > 65535 || cheight > 65535) {
 	    ignorechar(car, endofpacket);
 	    continue;
 	 }
-	 if (bmx) xoffs= get32() ;              /* horiz offset */
-	 if (bmy) yoffs= get32() ;              /* vert offset */
+	 xoffs= get32() ;              /* horiz offset */
+	 yoffs= get32() ;              /* vert offset */
+	 dprintf ("xoffs %d\n", xoffs);
+	 dprintf ("yoffs %d\n", yoffs);
       } else if (flagbyte > 3) {		/* extended short form */
 	 integer packetlength = ((flagbyte - 4)<<16) + get16() ;
 						/* packet length */
 	 car = pktopbm_pkbyte() ;			/* char number */
 	 endofpacket = packetlength + pktopbm_pkloc ;	/* calculate end of packet */
-	 if (car >= MAXPKCHAR) {
+         if ((car >= MAXPKCHAR) || !filename[car]) {
 	    ignorechar(car, endofpacket);
 	    continue;
 	 }
+	 dprintf ("flagbyte>3\n", 0);
+	 dprintf ("car: %d\n", car);
 	 pktopbm_pkbyte() ; 				/* ignore tfmwidth (3 bytes) */
 	 get16() ;				/* ignore tfmwidth (3 bytes) */
 	 get16() ;				/* ignore horiz escapement */
 	 cwidth = get16() ;			/* bounding box width */
 	 cheight = get16() ;			/* bounding box height */
-	 if (bmx)                               /* horiz offset */
-	    if ((xoffs=get16()) >= 32768)
-		xoffs-= 65536;
-	 if (bmy)                               /* vert offset */
-	    if ((yoffs=get16()) >= 32768)
-		yoffs-= 65536;
+	 dprintf ("cwidth %d\n", cwidth);
+	 dprintf ("cheight %d\n", cheight);
+	 xoffs=get16();                         /* horiz offset */
+	 if (xoffs >= 32768)
+	     xoffs-= 65536;
+	 yoffs=get16();                         /* vert offset */
+	 if (yoffs >= 32768)
+	     yoffs-= 65536;
+	 dprintf ("xoffs %d\n", xoffs);
+	 dprintf ("yoffs %d\n", yoffs);
       } else {					/* short form preamble */
 	 integer packetlength = (flagbyte<<8) + pktopbm_pkbyte() ;
 						/* packet length */
 	 car = pktopbm_pkbyte() ;			/* char number */
 	 endofpacket = packetlength + pktopbm_pkloc ;	/* calculate end of packet */
-	 if (car >= MAXPKCHAR) {
+         if ((car >= MAXPKCHAR) || !filename[car]) {
 	    ignorechar(car, endofpacket);
 	    continue;
 	 }
+	 dprintf ("flagbyte<=3\n", 0);
+	 dprintf ("car: %d\n", car);
 	 pktopbm_pkbyte() ; 			/* ignore tfmwidth (3 bytes) */
 	 get16() ; 				/* ignore tfmwidth (3 bytes) */
-	 pktopbm_pkbyte() ;                     /* ignore horiz escapement */
+	 x = pktopbm_pkbyte() ;                     /* ignore horiz escapement */
+	 dprintf ("horiz esc %d\n", x);
 	 cwidth = pktopbm_pkbyte() ;            /* bounding box width */
 	 cheight = pktopbm_pkbyte() ;           /* bounding box height */
-	 if (bmx)                               /* horiz offset */
-	    if ((xoffs=pktopbm_pkbyte()) >= 128)
-	       xoffs-= 256;;
-	 if (bmy)                               /* vert offset */
-	    if ((yoffs=pktopbm_pkbyte()) >= 128)
-	       yoffs-= 256;;
+	 dprintf ("cwidth %d\n", cwidth);
+	 dprintf ("cheight %d\n", cheight);
+	 xoffs=pktopbm_pkbyte ();               /* horiz offset */
+	 if (xoffs >= 128)
+	    xoffs-=256;
+	 yoffs=pktopbm_pkbyte ();               /* vert offset */
+	 if (yoffs >= 128)
+	    yoffs-=256;
+	 dprintf ("xoffs %d\n", xoffs);
+	 dprintf ("yoffs %d\n", yoffs);
       }
       if (filename[car]) {
 	 if (!bmx) bmx= cwidth;
@@ -304,10 +343,12 @@
 	 continue;
       }
       bitweight = 0 ;
+      for (i = 0 ; i < bmy ; i ++)           /* make it blank */
+	 for (j = 0 ; j < bmx ; j ++)
+	    bitmap[i][j]= PBM_WHITE;
       if (dynf == 14) {				/* bitmapped character */
-	 for (i = 0 ; i < bmy ; i ++)           /* make it blank */
-	    for (j = 0 ; j < bmx ; j ++)
-	       bitmap[i][j]= PBM_WHITE;
+	 dprintf ("bmy: %d\n ", bmy);
+	 dprintf ("y: %d\n ", bmy-yoffs-1);
 	 for (i = 0 ; i < cheight ; i ++) {
 	    int yi= i+(bmy-yoffs-1);
 	    for (j = 0 ; j < cwidth ; j ++) {
@@ -321,6 +362,8 @@
 	 integer hbit = cwidth ;
 	 integer rp = 0;
 	 repeatcount = 0 ;
+	 dprintf ("bmy: %d\n ", bmy);
+	 dprintf ("y: %d\n", cheight-rowsleft+(bmy-2*yoffs-1));
 	 while (rowsleft > 0) {
 	    integer count = pkpackednum() ;	/* get current colour count */
 	    while (count > 0) {
@@ -333,7 +376,7 @@
 		  while (hbit--)
 		     row[rp++] = turnon ? PBM_BLACK : PBM_WHITE;
 		  for (i = 0; i <= repeatcount; i++) {  /* fill row */
-		     int yi= i+cheight-rowsleft+(bmy-yoffs-1);
+		     int yi= i+cheight-rowsleft-1;
 		     if (0<=yi && yi < bmy)
 			for (j = 0; j < cwidth; j++) {
 			   int xj= j-xoffs;

