Package list pnm2ppa / scrub-obsolete/main image.c
scrub-obsolete/main

Tree @scrub-obsolete/main (Download .tar.gz)

image.c @scrub-obsolete/mainraw · history · blame

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
/***************************************************************************
    image.c  -  code for reading and parsing a pbm, pgm or ppm format file 
                also, rgb -> cymk conversion and black separation
                             -------------------
    begin                : Thu Jan 13 2000
    copyright            : (C) 2000 by pnm2ppa project
    email                : 
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include <math.h>
#define __IMAGE_C__

#include "ppa_syslog.h"
#include "global.h"
#include "defaults.h"

#include "debug.h"
#include "image.h"
#include "gamma.h"
#include "dither.h"
#include "lang.h"

#include <assert.h>



int
initImage (image_t *image, FILE *fptr)
{
    char line[1024];
    int colors = 0;
    int k, k1;

    image->fptr = fptr;
    image->version = none;
    image->blackCurLine = 0;
    image->colorCurLine = 0;
    image->bufferCurLine = 0;
    image->unread = 0;
    image->colors = 0;
    
    gCalibrate = false;
    gByteCount = 3;

    if (gGammaMode)
      {
	/* produce color calibration page, uses the  default papersize */
	image->version = P6_PPMRAW;
	image->width = gWidth;      
	image->height = gHeight;
	g300 = false;
      }
    else
      {
	
	if (fgets (line, 1024, fptr) == NULL)
	  return 0;
	line[strlen (line) - 1] = 0;
	
	/* we have to handle the six pnm formats .
	 */
	if (!strcmp (line, "P1"))
	  {
	    DPRINTF("input format is P1: pbm (ascii)\n");
	    image->version = P1_PBM;
	    if (!gPixmapMode)
	      {
		gColorMode = false ;
		gByteCount = 1;
	      }
	    else
		gFastMode = true;
	    
	    strcpy(gFormat,"BitMap");
	  }
	if (!strcmp (line, "P2"))
	  {
	    DPRINTF("input format is P2: pgm (ascii)\n");
	    image->version = P2_PGM;
	    if (!gPixmapMode)
	      {
		gColorMode = false ;
		gByteCount = 1;
	      }
	    else
		gFastMode = true;
	    
	    strcpy(gFormat,"GreyMap");
	  }
	if (!strcmp (line, "P3"))
	  {
	    DPRINTF("input format is P3: ppm (ascii)\n");
	    image->version = P3_PPM;
	    strcpy(gFormat,"PixMap");
	  }
	if (!strcmp (line, "P4"))
	  {
	    DPRINTF("input format is P4: pbm (binary)\n");
	    image->version = P4_PBMRAW;
	    if (!gPixmapMode)
	      {
		gColorMode = false ;
		gByteCount = 1;
	      }
	    else
		gFastMode = true;

	    strcpy(gFormat,"BitMap");
	  }
	if (!strcmp (line, "P5"))
	  {
	    DPRINTF("input format is P5: pgm (binary)\n");
	    image->version = P5_PGMRAW;
	    if (!gPixmapMode)
	      {
		gColorMode = false ;
		gByteCount = 1;
	      }
	    else
		gFastMode = true;
	    strcpy(gFormat,"GreyMap");
	  }
	if (!strcmp (line, "P6"))
	  {
	    DPRINTF("input format is P6: ppm (binary)\n");
	    image->version = P6_PPMRAW;
	    strcpy(gFormat,"PixMap");
	  }
	if (image->version == none)
	  {
	    DPRINTF("input format is not recognized as PNM\n");
	  snprintf(syslog_message,message_size,"initImage(): %s", 
		    gMessages[E_IM_BADFORMAT] );
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
	    return 0;
	  }

	

	/* initialize switch that is used to terminate further printing 
	 * if EOF is detected while reading the input image
	 */
	
	gTerminate = false ;
	
	do {
	  if (fgets (line, 1024, fptr) == NULL)
	    return 0;
	  line[strlen (line) - 1] = 0;
	  if (!strcmp (line, "# pnm2ppa calibration image")){
	    gCalibrate = true;
	    g300 = false;
	    DPRINTF("initImage(): Calibration Page input detected!\n");
	  }
	} while (line[0] == '#');	
	if (2 != sscanf (line, "%d %d", &image->width, &image->height))
	  return 0;

	/* use the image dimensions read from the pnm innput file to
	 * set gWidth and gHeight, and test that these are accepatable
	 * for the printer model
	 * this will mean that any user input or default papersizes
	 * will only be uses for calibration or gamma correction output
	 * which is not generated by pnm imput 
	 */


	/* if we treat the input as 300dpi, double image dimensions */
	if(g300) {
	  DPRINTF("300dpi input mode\n");
	  image->width *= 2;
	  image->height *= 2;
	}

	 gWidth = image->width ;
	 gHeight = image->height ;
	 /* test that papersize is acceptable */
	 if   (  (gWidth  > gmaxWidth  )
	      || (gWidth  < gminWidth  )  
	      || (gHeight > gmaxHeight )
	      || (gHeight < gminHeight )
	      )
	   {
	  snprintf(syslog_message,message_size,"initImage(): %s %dx%d @600dpi\n", 
		     gMessages[E_IM_BADPAPERSIZE],  gWidth, gHeight);
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
	    return 0;
	   }
	
	switch(image->version)
	  {
	  case P2_PGM:
	  case P3_PPM:
	  case P5_PGMRAW:
	  case P6_PPMRAW:
	    if (fgets (line, 1024, fptr) == NULL)
	      return 0;
	    if (1 != sscanf (line, "%d", &colors))
	      return 0;
	    /* colors should be 255, as the code is currently implemented */
	    if (colors != 255)
	      {
	  snprintf(syslog_message,message_size,"initImage(): %s %d\n", 
		     gMessages[E_IM_BADMAXVALUE], colors);
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		return 0;
	      }
	    break;
	  case P1_PBM:
	  case P4_PBMRAW:
	    colors = 1;
	    break;
	  case none:
	  default:
	  snprintf(syslog_message,message_size,"initImage(): %s", 
		   gMessages[E_IM_BADFORMAT]);
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
	    return 0;
	    break;
	  }
	
	image->colors = colors ;
	
	DPRINTF( "initImage: width=%d, height=%d, colors = %d,"
		 " ByteCount = %d\n", 
		 image->width, image->height, image->colors, gByteCount);
	    
    }
    /* initialize buffer */
    image->bufsize = 8192;
    image->bufpos = 0;
    image->buflines = 0;
    image->buftype = empty;
    for (k = 0; k < gMaxPass; k++)
      {
	image->buffer[k] = malloc (image->bufsize);
	if (!image->buffer[k])
	  {
	    for (k1 = 0; k1 < k; k1++)
	      free (image->buffer[k1]);
	    return 0;
	}
      }
    return 1;
    
}

int
remallocBuffer (image_t *image, size_t bytes)
{
    int linesize;
    unsigned char *newbuf;
    /* size_t newsize; */
    int newsize ;
    int k;

    if (image->buftype == bitmap)
	linesize = (image->width + 7) / 8;
    else if (image->buftype == color)
	linesize = (image->width / 2 + 7) / 8 * 3;
    else
	linesize = 0;

    /* if space is adequate, do nothing */
    if (image->bufpos + image->buflines * linesize + bytes < image->bufsize)
	return 0;

    /* if there's an offset, move the image at buffer[k][bufpos] to the
     * the beginning of the buffer, and reset the offset to 0.
     */

    if (image->bufpos != 0)
    {
	DPRINTF("Moving image[k][bufpos] to image[k][0]\n");
	for (k = 0; k < gMaxPass; k++)
	    memmove (image->buffer[k],
		     &image->buffer[k][image->bufpos], image->buflines * linesize);
	image->bufpos = 0;
	if (image->bufpos + image->buflines * linesize + bytes < image->bufsize)
	    return 0;
    }

    /* calculate the number of bits and add one to double the needed memory
       size.*/
    newsize = pow(2, (int)((log(image->buflines * linesize + bytes)/log(2))+1));

    DPRINTF("resizing memory from %d bytes to %d bytes\n",
	image->bufsize, newsize);

    for (k=0; k < gMaxPass; k++)
    {
	newbuf = malloc(newsize);
	if ( ! newbuf )
	{
	  snprintf(syslog_message,message_size, "remallocBuffer(): %s", 
		    gMessages[E_BADMALLOC]);
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		exit (1);
	}
	memcpy(newbuf, image->buffer[k], image->buflines * linesize);
	free(image->buffer[k]);
	image->buffer[k] = newbuf;
    }
    image->bufsize = newsize;

    return 0;
}


BOOLEAN
convert_black_data (image_t *image, unsigned char *line, unsigned char *black,
		    BOOLEAN line_is_nonblank)
{
  /* Lets document this - please correct anything incorrect here!
     (duncan, Feb 12 2000, updated July 2000)

     -  The entries in line[] are written by one of the
        functions FS_Color_Dither(), HT_Color_Dither(), HT_Black_Dither()
        in  dither.c

     -  The entries in line[] represent the ( image->width ) pixels on a line.
        Each pixel is represented by three consecutive entries
              (line[i] , line [i + 1] ,line [i + 2] ) , 
	where  i = 0, 3, 6 , ... ,  < (image->width * 3) 
        The three entries correspond to Cyan, Magenta, and Yellow ink.

     -  entries in line[] take values  1, 2,...,(gMaxPass + 1) , 0  or  255,
        where gMaxPass is the maximum number of passes of the color print head.
	* gMaxPass is only used in Color mode (gColorMode=true)
        * In normal color mode (gEcomode=false, gFastmode=false) gMaxPass = 4
        * In  Economy mode (gEcoMode = true) gMaxPass = 1 
	* In Fast Color mode (gFastMode = true) gMaxPass = 1

     -  values line [] =  1, 2, ..., gMaxPass  correspond to
        gMaxPass, gMaxPass-1, ..., 1 drops of ink of a given color (C, M,or Y).
     -  the value line [] =  0 also means  gMaxPass drops of ink
     -  the value line [] > gMaxPass  (usually = gMaxPass+1 or 255) 
        means 0 drops of color ink.

     -  an entry (0, 0, 0) corresponds to a black drop, which can
        either be a drop of black ink, or gMaxPass drops each of C,M and Y ink,
        If this is implemented with a drop of black ink, convert_black_data()
        changes (0,0,0) to (255,255,255), so it won't get repeated  using
        colored ink by the following call to convert_color_data().

     -  whitespace is coded by (255,255,255).

     - triplication of bytes is switched off when gByteCount == 1 

    Color data is only printed on even lines.   

    Black data is printed on even and odd lines, 
    except in Black-and-White Economy mode (gColorMode=false, 
    gEcoMode=true), when only even lines of black are printed.  
    If the odd line is not printed, its black data is added to 
    the next even line.
  */

  /*
    FEB 2002 (duncan)
    try to clean this up for pictures vs. text.
    identify regions of black surrounded by white space as text ONLY
    if they are less that "textwidth" dots wide.  Otherwise, they are
    treated as part of an image, and will try to use both ink types,
    if any color was found on the line.
  */

    int i, j, k;
    unsigned char mask = 0x80;
    unsigned char *pos = black;
    int blackness ;
    BOOLEAN use_color = false; 
    BOOLEAN has_black = false;

/* max_text_width is arbitary: make it configurable?
   textwidth = maximum width of a black region that is printed
   only with black ink when color is present on the line,
   on the assumption it is text, not image.
*/
    const int max_text_width = 8;  /* color dots at 300dpi */ 



    unsigned char curr_stuff= 0 ;/* 0 = white, 1 = not white (current pixel)*/ 
    unsigned char remove_black = 1, have_color = 0;
    unsigned char first_black=0 ,  second_black=0 ; /*controls black ink density*/

    blackness = gBlackness ;
    j = image->bufferCurLine ;

    /* black ink quantity control (duncan) */
    /* this should be extended to control color  ink printing
       of "black" pixels */
    if ( blackness == 1 )
      {
	/* one black ink drop, on even lines only */
	if ( (j % 2) ) 
	  {
	    first_black = 0   ; /* odd lines */
	    second_black =  0;
	  }
	else
	  {
	    first_black =  0 ; /* even lines */
	    second_black = 1 ;
	  }
      }
    else if ( blackness == 2 )
      /* two black ink drops  */
      {
	if ( (j % 2) ) 
	  {
	    first_black = 1   ; /* odd lines */
	    second_black =  0;
	  }
	else
	  {
#if 0
	    first_black =  0 ;  /* even lines */
	     second_black = 1 ; 
#endif
	    /* treat odd and even lines the same, to avoid artifacts?*/ 
	    first_black =  1 ; /* even lines */
	    second_black = 0 ;
	  }
      }
    else if ( blackness == 3 )
      /* two drops on even lines, one on odd lines  */
      {
	first_black = 1;
      if ( j % 2 )
	  second_black = 0;  /* odd lines */
      else
	  second_black = 1 ; /* even lines */
	}
    else if (blackness == 4 )
      /* two black ink drops, all lines */
	{
	  first_black =  1 ;
	  second_black = 1 ;
	}
    else
      /* no black ink */
	{
	  first_black =  0  ;
	  second_black = 0  ;
	}
    
    /* In Pixmap mode  black ink cartridge is disabled (this should
       be renamed to (say) NoBlack mode ) */

    if (gPixmapMode || !line_is_nonblank)
      memset (pos, 0x00, (image->width + 7) / 8);
    else
      {
	if (gColorMode && gByteCount ==3 )
	  /* beginning of code that assumes gByteCount == 3 */
	  {
	    /* This is the most delicate situation, we (may) have both color 
	     *   and black on the same line; 
	     *  we want to:
	     *   - Print half of the black ink to avoid "wet" effect
	     *   - get a good "covering" black in pixmaps (neither too dense, 
	     *      neither too sparse...)  
	     */

	    /* check for presence of color in the line */

	    for (i = 0; i < image->width * 3; i += 6)
	      {
		if (line[i] != line[i+1] || line[i+1] != line[i+2])
		  {
		    use_color = true;
		    break;
		  }
	      }

	    curr_stuff=0;
	    
	    for (i = 0; i < image->width * 3; i += 6)
	      {
		if (mask == 0x80)
		  *pos = 0;

		if (curr_stuff==0)
		  {
		    int black_width = 0;
		    /* this is a test to see if the current non-whitespace
		     *  pixel is part of a region of black delineated by
		     * whitespace (have_color=0)
		     * or is in a region containing some
		     * colored pixels (in this case have_color=1) 
		     */
		    if (! (line[i]>gMaxPass &&   /*if line is not white*/
			   line[i+1]> gMaxPass && 
			   line[i+2]>gMaxPass) )
		      {

			have_color=0;
			k=i;
			while ( (!(have_color)) &&
				(k<image->width * 3) &&
				(! (line[k]>gMaxPass &&
				    line[k+1]> gMaxPass && 
				    line[k+2]>gMaxPass) ))
			  { 
			    if ((line[k] && line[k]<=gMaxPass) ||
				(line[k+1] && line[k+1]<=gMaxPass) ||
				(line[k+2] && line[k+2]<=gMaxPass))
			      have_color=1;   
			    
			    k+=6;
			    black_width++;
			    /* treat wide black regions as having color */
			    if (use_color && black_width  > max_text_width)
			      have_color=1;
			  }
			if (have_color) 
			  remove_black=0;
			else
			  remove_black=1;
			curr_stuff=1;
		      }
		  }
  

		if ( !(line[i] || line[i + 1] || line[i + 2]))
		  {
		    /* pixel is black; there are two possible
		     * behaviors, depending on whether remove_black
		     * is set by Drugo's new code above.
		     * these behaviors may need some further
		     * adjustments.
		     */
		    if (gCalibrate || remove_black)
		      {
			/* in this case  the black region is 
			 * identified as  delineated by whitespace
			 * (probably =  text?).   
			 * So print with black ink, 
			 * and no colored ink. To avoid  using
			 * too much black ink (wets paper)
			 * first_black and second_black
			 *  are used as controls on amount
			 * of black ink.
			 */
			if  (first_black && gBlack ) 
			  *pos |= mask;   /* one drop of black ink */
			mask >>= 1;
			if (mask == 0)
			  {
			    pos++;
			    mask = 0x80;
			  }
			if  (second_black && gBlack ) 
			  *pos |= mask;   /* one  drop of black ink */ 
			/*  suppress use of colored ink on this pixel */
			line[i] = line[i + 1] = line[i + 2] = 255;
			line[i + 3] = line[i + 4] = line[i + 5] = 255;
		      }
		    else
		      {
			/* this black region is bounded by a colored
			 * region, and is probably part of an image.
			 * so: print half strength black ink, and use
			 * some colored ink.
			 *
			 * note: in  EcoMode, only even lines are processed.
			 * in normal mode, skip even lines.
			 * j is the line number.
			 */
			if ( (j % 2 || gEcoMode ) && gBlack ) 
			  *pos |= mask;/* a drop of black, every second line */
			mask >>= 1;
			if (mask == 0)
			  {
			    pos++;
			    mask = 0x80;
			  }
			/* reduce amount  of colored ink to avoid wetting:
			 * try  resetting line[] to gMaxPass-1 
			 * gives 2  passes  of the color head in high quality,
			 * (gMaxPass=4, line[] = 3),
			 * 1 pass in ecoMode (gMaxPass = 1, line[] = 0)
			 */
			line[i] = line[i + 1] = line[i + 2] = gMaxPass-1;
			line[i + 3] = line[i + 4] = line[i + 5] = gMaxPass-1;
		      }
		  }
		else 
		  {
		    /* pixel is not black */
		    mask >>= 1;
		    if (mask == 0)
		      {
			pos++;
			mask = 0x80;
		      }
		    if (line[i] > gMaxPass && 
			line[i + 1] > gMaxPass && 
			line[i + 2] > gMaxPass)
		      {
			/* pixel is whitespace */
		      curr_stuff=0;
		      }
		  }
				
		mask >>= 1;
		if (mask == 0)
		  {
		    pos++;
		    mask = 0x80;
		  }
	      }
	  }    /* end of code that assumes gByteCount == 3 */

	else  /* !gColorMode, gByteCount could be  1 or 3 */
	  {
	    /* this next part is for printing in black and white only.
	     * if !gColorMode,  the line contains only black or white, so 
	     * we can print safely 1 drop out of 2 without any problem 
	     */

	    *pos = 0;

	    for (i = 0 ; i < image->width *gByteCount ; i += 2*gByteCount )
	      {
		if (mask == 0x80)
		  *pos = 0;

		if (!line[i])     /* black even pixel */
		  {
		    if  (first_black && gBlack) 
		      *pos |= mask;   /* one drop of black ink */
			mask >>= 1;
			if (mask == 0)
			  {
			    pos++;
			    mask = 0x80;
			  }
			if  (second_black && gBlack) 
			  *pos |= mask;   /* one  drop of black ink */ 
		  }
		else  /* blank even pixel */
		  {
		    mask >>= 1;
		    if (mask == 0)
		      {
			pos++;
			mask = 0x80;
		      }
		  }

		mask >>= 1;    /* odd pixels are left black */
		if (mask == 0)
		  {
		    pos++;
		    mask = 0x80;
		  }
	      }
	    memset (line, 0xff, image->width );
	  }
	
      }

    if (!(gPixmapMode || !line_is_nonblank)) {
      unsigned char *p;
      for (p = black; p <= pos; p++) {
	if( *p ) {
	  has_black = true;
	  break;
	}
      }
    }

    return has_black;
}


BOOLEAN
convert_color_data (image_t *image, unsigned char *line,
		    unsigned char *color[], int c_off,
		    BOOLEAN line_is_nonblank)
{
    int i, k, j;
    unsigned char mask;
    unsigned char *pos;
    BOOLEAN has_color = false;

    j = 1;
    for (k = 0; k < gMaxPass; k++)
    {
	mask = 0x80;
	pos = &color[gMaxPass - j][c_off];
	j++;

	memset (pos, 0x00, (image->width / 2 + 7) / 8 * 3);

	if (line_is_nonblank) 
	  for (i = 0; i < image->width * 3; i += 6)
	    {
	      
	      if ((line[i] < gMaxPass - k + 1) &&  gCyan)
		pos[0] |= mask;
	      if ((line[i + 1] < gMaxPass - k + 1) && gMagenta)
		pos[1] |= mask;
	      if ((line[i + 2] < gMaxPass - k + 1) && gYellow)
	      pos[2] |= mask;
	      
	      mask >>= 1;
	      if (mask == 0)
		{
		  pos += 3;
		  mask = 0x80;
		}
	    }



	if (line_is_nonblank && k == 0) {
	  unsigned char *p;
	  for (p = &color[gMaxPass -1][c_off]; p <= pos; p++) {
	    if( *p) {
	      has_color = true;
	      break;
	    }
	  }
	  
	}
    }
    return has_color;
}

/* read a line of data from the file and convert to the necessary format */
BOOLEAN
read_line (image_t * image, unsigned char *black[], int b_off,
	   unsigned char *color[], int c_off)
{
  static unsigned char *oldline = NULL;  /* take care to free this! */
  unsigned char *prevline = NULL;  /* take care to free this! */
  unsigned char *line = NULL; /* take care to free this! */
  int i, j, k ;
  int fread_eof = 0;
  BOOLEAN line_is_nonblank = true;      
  static BOOLEAN prevline_is_nonblank = true;      
  int image_width = image->width;
  const BOOLEAN repeat_line = (g300  && image->bufferCurLine %2);

  /* 
     for 300dpi input (g300 is true) , 
     the data is duplicated horizontally and vertically
     to convert it to 600dpi format. 
     odd lines are copies of the preceeding even line, 
     which was stored as "oldline" :
  */
  if(g300) { 
    image_width /= 2;
    if (repeat_line) {
      /* in 300dpi input mode,
	 the data from the previous even line should be used again
	 for this odd line*/
      assert(oldline);
      line = oldline;
      oldline = NULL;
    }
  }
    
  if(!line)
    if (!(line = malloc (image->width * gByteCount)))
      {
	/* make this nicer... propagate upwards */
	snprintf(syslog_message,message_size, "read_line(): %s", 
		 gMessages[E_BADMALLOC]);
	wrap_syslog (LOG_CRIT,"%s",syslog_message); 
	if (oldline) 
	  free (oldline);
	exit (1);
      }
  
  
  /* All the six pnm input formats 
   * (pbm, pbmraw, pgm, pgmraw, ppm, ppmraw ) 
   * can be read from the input, and  converted to 
   * ppmraw format for further processing.   
   */


  if (!gGammaMode && !gTerminate && !repeat_line)
    {
      switch (image->version)
	{
	  /*  for completeness, the non-raw pnm formats are  supported */
	case P1_PBM:
	  {
	  /* read pbm format; convert to ppm  format if gByteCount = 3 */
	    /* This is included for completeness, 
	     * but this implementation is PAINFULLY slow !
	     * is there a better way?
	     * (YES, USE THE PPMRAW FORMAT ONLY !)
	     */
	    int index = 0;
	    int incr = gByteCount;
	    if(g300)
	      incr *= 2;
	    for ( i = 0 ; i < image_width  ; )
	      {
		switch( getc( image->fptr ) )
		  {
		  case EOF:
		    snprintf(syslog_message,message_size, "read_line(pbm,ascii): %s PBM\n", 
			     gMessages[E_IM_EOFREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    
		    /* input data is truncated; set gTerminate mode
		     * and replace lost part of image with whitespace
		     * until print run ends.
		     */
		    gTerminate = true ;
		    break;
		  case '1':
		    line[index] = 0;
		    index += incr;
		    i ++;
		    break;
		  case '0':
		    line[index] = 255;
		    index += incr;
		    i ++;
		    break;
		  default :
		    break;
		  }
	      }
	    if(g300) {
	      for ( j = 0 ; j < index  ; j += incr )
		line[j+gByteCount] = line[j];
	    }
	    if (gByteCount == 3) {
	      for (j = 0; j < index ; j += 3 )
		line[j + 2]= line[j + 1] = line[j];
	      }
	  }
	  break;
	case P2_PGM:
	  {
	  /* read pgm format; convert to ppm  format if gByteCount = 3 */
	    /* This is included for completeness, 
	     * but this implementation is PAINFULLY slow !
	     * is there a better way?
	     * (YES, USE THE PGMRAW FORMAT ONLY !)
	     */
	    int index = 0;
	    unsigned int value ;
	    int incr = gByteCount;
	    if(g300)
	      incr *= 2;
	    for ( i = 0 ; i < image_width  ; i ++ )
	      {
		switch(fscanf(image->fptr, "%u ", &value )) 
		  {
		  case 1:
		    break ;
		  case EOF:
		    snprintf(syslog_message,message_size,"read_line(pgm,ascii): %s PGM\n",
			     gMessages[E_IM_EOFREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    
		    /* input data is truncated; set gTerminate mode
		     * and replace lost part of image with whitespace
		     * until print run ends.
		     */
		    gTerminate = true ;
		    break;
		  default:
		    snprintf(syslog_message,message_size,"read_line(pgm,ascii): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    line = NULL;
		    if (oldline) {
		      free (oldline);
		      oldline = NULL;
		    }
		    exit (1);
		    break;
		  } 
		if(value <= 255)
		  {
		    line[index] =  value;
		    index += incr;
		}
		else
		  {
		    snprintf(syslog_message,message_size,"read_line(pgm,ascii): %s", 
			     gMessages[E_IM_BADIMAGE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (oldline) 
		      free (oldline);
		    exit (1);
		  }
	      }
	    if(g300) {
	      for ( j = 0 ; j < index  ; j += incr )
		line[j+gByteCount] = line[j];
	    }
	    if (gByteCount == 3) {
		for (j = 0; j < index ; j += 3 )
		  line[j + 2]= line[j + 1] = line[j];
	      }
	  }
	  break;
	case P3_PPM:
	  {
	  /* read pbm format */
	    /* This is included for completeness, 
	     * but this implementation is PAINFULLY slow !
	     * is there a better way?
	     * (YES, USE THE PPMRAW FORMAT ONLY !)
	     */
	    unsigned int value ;
	    int index = 0;
	    assert(gByteCount == 3);
	    for ( i = 0 ; i < image_width * 3  ; i ++   )
	      {
		switch  (fscanf(image->fptr, "%u ", &value ) )
		  {
		  case 1:
		    break ;
		  case EOF:
		    snprintf(syslog_message,message_size,"read_line(ppm,ascii): %s PPM\n", 
			     gMessages[E_IM_EOFREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 

		    /* input data is truncated; set gTerminate mode
		     * and replace lost part of image with whitespace
		     * until print run ends.
		     */
		    gTerminate = true ;
		    break;
		  default:
		    snprintf(syslog_message,message_size,"read_line(ppm,ascii): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (oldline) 
		      free (oldline);
		    exit (1);
		    break;
		  }
		if(value <= 255){
		  if (g300) {
		    line[index+3] = line[index] = value;
		    index++;
		    if(!(index %3))
		      index+=3;
		  } else {
		    line[index] = value ;
		    index ++;
		  }
		}
		else
		  {
		    snprintf(syslog_message,message_size,"read_line(ppm,ascii) %s", 
			     gMessages[E_IM_BADIMAGE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (oldline) 
		      free (oldline);
		    exit (1);
		  }
	      }
	  }
	  break;
	case P4_PBMRAW:
	  /* read pbmraw format; convert to ppm  format if gByteCount = 3 */
	  {
	    int pbm_width, count, index=0;
	    int incr = gByteCount;
	    char* pbmraw_data;
	    unsigned char mask[8] = 
	      { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
	    pbm_width = ( image_width + 7 ) / 8;
	    
	    if(g300)
	      incr *= 2;
	    if (! (pbmraw_data = malloc(pbm_width)) )
	      {
		snprintf(syslog_message,message_size,"read_line(pbmraw): %s", 
			 gMessages[E_BADMALLOC]);
		wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		if (oldline) 
		  free (oldline);
		free (line);
		exit (1);
	      }
	    
	    count = fread(pbmraw_data, 1 , pbm_width, image->fptr);
	    if (pbm_width == count)
	      {
		/* success */
		/* pbm raw format: 0=white, 1=black;
		 * bits are stored eight per byte, high bit
		 * first, low bit last. 
		 */
		index = 0;
		for ( i = 0 ; i < image_width ;  i ++  )
		  {
		    k = i % 8 ;
		    j = i / 8 ;
		    if ( pbmraw_data[j] & mask[k] )
		      line[index] = 0;
		    else
		      line[index] = 255;
		    index += incr;
		  }
		free(pbmraw_data);
	      }
	    else
	      {
		/* error reading line */
		free(pbmraw_data);
		fread_eof=feof(image->fptr);
		clearerr(image->fptr);
		if (fread_eof)
		  {
		    snprintf(syslog_message,message_size,"read_line(pbmraw): %s PBMRAW\n", 
			     gMessages[E_IM_EOFREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    
		    /* input data is truncated; set gTerminate mode
		     * and replace lost part of image with whitespace
		     * until print run ends.
		     */
		    gTerminate = true ;
		  }
		else
		  {
		    snprintf(syslog_message,message_size,"read_line(pgmraw): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (oldline)
		      free (oldline);
		    exit (1);
		  }
	      }
	    if(g300) {
	      for ( j = 0 ; j < index  ; j += incr )
		line[j + gByteCount] = line[j];
	    }
	    if (gByteCount == 3) {
	      for (j = 0; j < index ; j += 3 )
		line[j + 2]= line[j + 1] = line[j];
	    }
	  }	  
	  break;
	case P5_PGMRAW:
	  
	  /* read pgmraw format; convert to ppm  format if gByteCount = 3 */
	  {
	    int  index=0 ;
	    char* pgm_data;
	    int incr = gByteCount;
	    if(g300)
	      incr *= 2;
	    pgm_data = malloc(image_width);
	    if (!pgm_data)
	      {
		snprintf(syslog_message,message_size,"read_line(pgmraw): %s", 
			 gMessages[E_BADMALLOC]);
		wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		free (line);
		if (oldline)
		  free (oldline);
		exit (1);
	      }
	    if ( 1 == fread (pgm_data, image_width , 1, image->fptr) )
	      {
		 /* success */
		index = 0 ;
		for ( i = 0 ; i < image_width ;  i ++  )
		  {
		    line[index] = pgm_data[i] ;
		    index += incr;
		  }
		free(pgm_data);
	      }
	     else
	       {
		 /* error reading line */
		 free(pgm_data);
		 fread_eof=feof(image->fptr);
		 clearerr(image->fptr);
		 if (fread_eof)
		   {
		     snprintf(syslog_message,message_size,"read_line:(pgmraw) %s PGMRAW\n", 
			      gMessages[E_IM_EOFREADLINE]);
		     wrap_syslog (LOG_CRIT,"%s",syslog_message); 

		     /* input data is truncated; set gTerminate mode
		      * and replace lost part of image with whitespace
		      * until print run ends.
		      */
		     gTerminate = true ;
		   }
		 else
		   {
		     snprintf(syslog_message,message_size,"read_line(pgmraw): %s", 
			      gMessages[E_IM_BADREADLINE]);
		     wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		     free (line);
		     if (oldline) 
		       free (oldline);
		     exit (1);
		   }
	       }

	    if(g300) {
	      for (j = 0 ; j < index  ; j += incr )
		line[j + gByteCount] = line[j];
	    }
	    if (gByteCount == 3){
	      for (j = 0; j < index ; j += 3 )
		line[j + 2]= line[j + 1] = line[j];
	    }
	  }
	  break;
	case P6_PPMRAW:
	  /* read ppmraw format */
	  {
	    assert(gByteCount == 3);
	    if (1 !=  fread (line, image_width * 3, 1, image->fptr) )
	      {
		fread_eof=feof(image->fptr);
		clearerr(image->fptr);
		if (fread_eof)
		  {
		    snprintf(syslog_message,message_size,"read_line(ppmraw):%s \n", 
			     gMessages[E_IM_EOFREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    
		    /* input data is truncated; set gTerminate mode
		     * and replace lost part of image with whitespace
		     * until print run ends.
		     */
		    gTerminate = true ;
		  }
		else
		  {
		    snprintf(syslog_message,message_size,"read_line(ppmraw): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (oldline) 
		      free (oldline);
		    exit (1);
		  }
	      }
	    /* if input data was at 300dpi, expand it to 600dpi format */
	      if(g300) {
		int j,k;
		for ( i = image_width - 1  ; i > 0  ; i--) {
		  j = 3 * i ;
		  k = 2 * j;
		  line[ k + 3 ] = line[ k     ] = line[ j     ];
		  line[ k + 4 ] = line[ k + 1 ] = line[ j + 1 ];
		  line[ k + 5 ] = line[ k + 2 ] = line[ j + 2 ];
		}
		line[3] = line[0];
		line[4] = line[1];
		line[5] = line[2];
	      }  
	  }
	  break;
	case none:
	default:
	  snprintf(syslog_message,message_size,"read_line(?): %s", 
		   gMessages[E_IM_BADFORMAT]);
	  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
	  free (line);
	  if (oldline)
	    free (oldline);
	  exit (1);
	  break;
	}
    }
  else  if (gGammaMode) 
    ProduceGamma (image, line ) ;  /* generate color calibration page */

  if ( gTerminate || 
       image->bufferCurLine < image->top ||
       image->bufferCurLine >= image->height - image->bottom ) {
    /* 
     * gTerminate becomes true if EOF was obtained when a line was read .
     * The input image is incomplete .
     * Print only previously-read complete lines.
     * unread lines are replaced with blanks for the rest of the print run
     *
     * also blank out lines above the top margin,
     * or below the bottom margin.
     */
    line_is_nonblank = false;
    memset(line, 0xff, image->width * gByteCount);
  } 

  if(line_is_nonblank) { 
    if (repeat_line)
      line_is_nonblank = prevline_is_nonblank ;
    else {
      /* crop image to left and right margins */
      int imin = 0, imax = image->width * gByteCount;
      if(image->left > 0) {
	imin = image->left * gByteCount;
	memset(line,0xff, imin);
      }
      if(image->right > 0) {
	imax =  (image->width - image->right) * gByteCount;
	memset(line + imax, 0xff, image->right * gByteCount);
      }
      /* check if cropped  line is really nonblank */
      line_is_nonblank = false;
      for ( i = imin ; i < imax ; i++ ) {
	if (line[i] != 255) {
	  line_is_nonblank = true;
	  break;
	}
      }
    }
  }

  
  /* mark line as read */
  image->LineType[image->bufferCurLine] = 
    image->LineType[image->bufferCurLine] | READLINE;
  
  
  if (!gTerminate && g300 && !repeat_line){
    /* in 300dpi mode, store the data when bufferCurLine is even,
       to use it again for the next odd line at 600dpi */

    if (!(prevline = malloc (image->width * gByteCount))) {
      /* make this nicer... propagate upwards */
      snprintf(syslog_message,message_size, "read_line(): %s", 
	       gMessages[E_BADMALLOC]);
      wrap_syslog (LOG_CRIT,"%s",syslog_message);
      free(line);
      if (oldline) 
	free (oldline);
      exit (1);
    }

    prevline_is_nonblank = line_is_nonblank;
    if (prevline_is_nonblank)
      memcpy(prevline, line, image->width * gByteCount);
    else
      memset(prevline, 0xff, image->width * gByteCount);
  }
  
  
  /* just to be safe ... */
  if(gTerminate && oldline) {
    free(oldline);
    oldline = NULL;
  }
  
  /* process ALL non-blank lines to avoid artifacts 
     (odd lines are only processed for black in *_Dither() functions )
     hopefully, this minimal processing is quite fast */
  
  
  if(line_is_nonblank) {
    image->LineType[image->bufferCurLine] = 
      image->LineType[image->bufferCurLine] |  NONBLANK;
    if (gColorMode)
      {
	if (gFastMode)
	  HT_Color_Dither (image, line);
	else
	  FS_Color_Dither (image, line);
      }
    else
      HT_Black_Dither (image, line);
  }    
  
  

    /* if the current line is odd, we only need to return black data */
  if (image->bufferCurLine % 2 == 1)
    {
      if  ( (!gEcoMode || gColorMode) && !gPixmapMode  )
	{
	  if (convert_black_data 
	      (image, line, &black[0][b_off],line_is_nonblank))
	    image->LineType[image->bufferCurLine] =
	      image->LineType[image->bufferCurLine] |  BLACKLINE;
 	}
      else
	{
	  /* in B&W EcoMode, or in PixmapMode,
	   *  don't print the odd line, but store  it
	   * (unless this is the last line!)
	   */
	  if ( image->bufferCurLine + 1 < image->height )
	    {
	      memset(&black[0] [b_off], 0x00, (image->width +7 ) / 8 );
	      oldline = malloc (image->width * gByteCount);
	      memcpy(oldline, line, image->width * gByteCount);
	    }
	}
    }
  else
    {
      /* return both black and color data on even lines */
      /* note that convert_color_data must be called AFTER convert_black_data */
      if ( oldline )
	{
	  /* add any black from previous unprinted odd line 
	     to next even line */
	  if (gByteCount == 3)
	    {
	      for ( i = 0; i < image->width * 3; i += 3 )
		{
		  if ( !oldline[i] && !oldline[i + 1] && !oldline[i + 2] )
		    line[i + 2] = line[i + 1] = line[i] = 0 ;
		}
	    }
	  else  /* gByteCount = 1 */
	    {
	      for ( i = 0; i < image->width ; i ++ )
		{
		  if ( !oldline[i] )
		    line[i] = 0 ;
		}
	    }

	  free(oldline);  
	  oldline = NULL;
	}
      
      if (convert_black_data 
	  (image, line, &black[0][b_off],line_is_nonblank))
	image->LineType[image->bufferCurLine] = 
	  image->LineType[image->bufferCurLine] | BLACKLINE;

      if (gColorMode)
	if (convert_color_data 
	    (image, line, color, c_off,line_is_nonblank))
	  image->LineType[image->bufferCurLine] =
	    image->LineType[image->bufferCurLine] | COLORLINE;
    }
  
  
  free (line);

  if(prevline) {
    assert(!oldline);
    oldline = prevline;
  }


  image->bufferCurLine++;

  return line_is_nonblank;
}

/* Drugo */


/* buffer a black line or two and return color data */

void
buffer_black_line (image_t *image, unsigned char *data[], int offset)
{
    int lines, linesize = (image->width + 7) / 8;
    BOOLEAN nonblank = false;
    int curline = image->bufferCurLine;
   

    /* if the current line is odd, we need to read 2 black lines */
    if (image->bufferCurLine % 2 == 1)
	lines = 2;
    else
	lines = 1;		

    remallocBuffer (image, linesize * lines);

    /* read_line into buffer and 'data' */
    read_line (image, image->buffer,
	       image->bufpos + image->buflines * linesize, data, offset);
    if (image->LineType[curline] & BLACKLINE)
      nonblank = true;

    image->buflines++;

    /* if we have to read 2 lines, then we haven't gotten any color data yet */
    if (lines == 2)
    {
      curline++;
      read_line (image, image->buffer,
		 image->bufpos + image->buflines * linesize, data, offset);
      image->buflines++;
      if (image->LineType[curline] & BLACKLINE)
	nonblank = true;
    }

    /* if no black data exists in buffer, advance blackCurLine */
    if (image->buftype == empty && !nonblank ) {
      image->buflines = 0;
      image->blackCurLine += lines;             ;
    } else
      image->buftype = bitmap;
}

/* buffer a color line and return black data */
void
buffer_color_line (image_t *image, unsigned char *data[], int offset)
{
    int linesize = (image->width / 2 + 7) / 8 * 3;	
    BOOLEAN nonblank = false;
    int curline = image->bufferCurLine;

    /* if the current line is odd, we don't need to buffer any color data */
    if (image->bufferCurLine % 2 == 1)
    {
	read_line (image, data, offset, NULL, 0);
	return;
    }

    /* we need to buffer 1 color line */
    if (gColorMode)
	remallocBuffer (image, linesize);

    read_line (image, data, offset, image->buffer,
	       image->bufpos + image->buflines * linesize);
    if (image->LineType[curline] & COLORLINE)
      nonblank = true;
    image->buflines++;

    /* if no color data exists in buffer, advance colorCurLine */
    if (image->buftype == empty && !nonblank ) {
      image->buflines = 0;
      image->colorCurLine += 2;
    } else
      image->buftype = color;
}

/* Reads a single line of black image data into data which must be at least
   (image->width+7)/8 bytes of storage */
int
im_black_readline (image_t *image, unsigned char *data[], int offset)
{

    if (image->blackCurLine >= image->height)
      return 0;

    if (image->unread)
    {
	memcpy (data[0] + offset, image->revdata, (image->width + 7) / 8);
	image->blackCurLine++;
	image->unread = 0;
	free (image->revdata);
	return 1;
    }
    
    /* if there are buffered black lines, use one of them */
    /* if there aren't buffered black lines, process the next line and */
    /*   buffer a color line */
    if (image->buftype == bitmap)
      {
	memcpy (data[0] + offset,
		&image->buffer[0][image->bufpos], (image->width + 7) / 8);
	image->bufpos += (image->width + 7) / 8;
	if (--image->buflines == 0)
	  {
	    image->bufpos = 0;
	    image->buftype = empty;
	  }
      }
	else
	  buffer_color_line (image, data, offset);
    image->blackCurLine++;
    return 1;
    
}

/* Reads a single line of color image data into data which must be at least
   (image->width/2+7)/8*3 bytes of storage */
int
im_color_readline (image_t *image, unsigned char *data[], int offset)
{
    int k;

    if (image->colorCurLine >= image->height)
    {
	DPRINTF ("im_color_readline returning 0 immediately!\n");
	return 0;
    }
    
    /* if there are buffered color lines, use one of them */
    /* if there aren't buffered color lines, process the next line and */
    /*   buffer a black line */
    if (image->buftype == color)
      {
	for (k = 0; k < gMaxPass; k++)
	  memcpy (data[k] + offset,
		  &image->buffer[k][image->bufpos], 3 * ((image->width / 2 + 7) / 8));
	image->bufpos += 3 * ((image->width / 2 + 7) / 8);
	image->buflines--;
	if (image->buflines == 0)
	  {
	    image->bufpos = 0;
	    image->buftype = empty;
	  }
      }
    else
      buffer_black_line (image, data, offset);
    image->colorCurLine += 2;
    return 1;
}

/* push a black line back into the buffer; we read too much! */
void
im_unreadline (image_t * image, void *data)
{
    /* can only store one line in the unread buffer */
    if (image->unread)
	return;

    image->unread = 1;
    image->revdata = malloc ((image->width + 7) / 8);
    memcpy (image->revdata, data, (image->width + 7) / 8);
    image->blackCurLine--;
}