Codebase list pnm2ppa / debian/1.04-4 image.c
debian/1.04-4

Tree @debian/1.04-4 (Download .tar.gz)

image.c @debian/1.04-4raw · 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
/***************************************************************************
    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.                                   *
 *                                                                         *
 ***************************************************************************/


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

#include "syslog.h"
#include "global.h"
#include "defaults.h"

#include "debug.h"
#include "image.h"
#include "gamma.h"
#include "dither.h"
#include "lang.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;
    
    if (gGammaMode)
      {
	/* produce color calibration page, uses the  default papersize */
	image->version = P6_PPMRAW;
	image->width = gWidth;      
	image->height = gHeight;
      }
    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"))
	  {
	    image->version = P1_PBM;
	    if (!gPixmapMode)
	      gColorMode = false ;
	    strcpy(gFormat,"BitMap");
	  }
	if (!strcmp (line, "P2"))
	  {
	    image->version = P2_PGM;
	    if (!gPixmapMode)
	      gColorMode = false ;
	    strcpy(gFormat,"GreyMap");
	  }
	if (!strcmp (line, "P3"))
	  {
	    image->version = P3_PPM;
	    strcpy(gFormat,"PixMap");
	  }
	if (!strcmp (line, "P4"))
	  {
	    image->version = P4_PBMRAW;
	    if (!gPixmapMode)
	      gColorMode = false ;
	    strcpy(gFormat,"BitMap");
	  }
	if (!strcmp (line, "P5"))
	  {
	    image->version = P5_PGMRAW;
	    if (!gPixmapMode)
	      gColorMode = false ;
	    strcpy(gFormat,"GreyMap");
	  }
	if (!strcmp (line, "P6"))
	  {
	    image->version = P6_PPMRAW;
	    strcpy(gFormat,"PixMap");
	  }
	if (image->version == none)
	  {
	  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;
	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 
	 */

	 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\n", 
		 image->width, image->height, image->colors);
	    
    }
    /* 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;
}



void
convert_black_data (image_t *image, unsigned char *line, unsigned char *black)
{
  /* 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).

    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.
  */


    int i, j, k;
    unsigned char mask = 0x80;
    unsigned char *pos = black;
    int blackness ;
    
    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
	  {
	    // first_black =  0 ; /* even lines */
	    // second_black = 1 ;
	    /* 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)
      memset (pos, 0x00, (image->width + 7) / 8);
    else
      {
	if (gColorMode)
	  {
	    /* 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...)  
	     */

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

		if (curr_stuff==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;
			  }
			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 (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;
		  }
	      }
	  }
	else
	  {
	    /* 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 * 3; i += 6)
	      {
		if (mask == 0x80)
		  *pos = 0;

		if ((!line[i] && !line[i + 1] && !line[i + 2]))
		  {
		    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 
		  {
		    /* pixel is not black */
		    mask >>= 1;
		    if (mask == 0)
		      {
			pos++;
			mask = 0x80;
		      }
		  }
		mask >>= 1;
		if (mask == 0)
		  {
		    pos++;
		    mask = 0x80;
		  }
	      }
	    memset (line, 0xff, image->width * 3);
	  }
	
      }
}


void
convert_color_data (image_t *image, unsigned char *line,
		    unsigned char *color[], int c_off)
{
    int i, k, j;
    unsigned char mask;
    unsigned char *pos;

    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);

	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;
	    }
	}
    }
}

/* Drugo */

/* read a line of data from the file and convert to the necessary format */
void
read_line (image_t * image, unsigned char *black[], int b_off,
	   unsigned char *color[], int c_off)
{
  static unsigned char *line, *oldline;  /* take care to free these! */
  int i, j, k ;
  int fread_eof = 0;
  static int have_oldline = 0;

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

  /* All the six pnm input formats 
   * (pbm, pbmraw, pgm, pgmraw, pbm, pbmraw ) 
   * can be read from the input, and  converted to 
   * ppmraw format for further processing.   
   */


  if (!gGammaMode && !gTerminate)
    {
      switch (image->version)
	{
	  /*  for completeness, the non-raw pnm formats are  supported */
	case P1_PBM:
	  for ( i = 0 ; i < image->width * 3  ; )
	    switch( getc( image->fptr ) )
	      {
	      case EOF:
		snprintf(syslog_message,message_size, "read_line(): %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[i] = line[i + 1]= line[i + 2] = 0;
		i += 3 ;
		  break;
	      case '0':
		line[i] = line[i + 1]= line[i + 2] = 255;
		i += 3 ;
		  break;
	      default :
		break;
	      }
	  break;
	case P2_PGM:
	  {
	    /* This is included for completeness, 
	     * but this implementation s PAINFULLY slow !
	     * is there a better way?
	     * (YES, USE THE PGMRAW FORMAT ONLY !)
	     */
	    unsigned int value ;
	    for ( i = 0 ; i < image->width * 3  ; i += 3 )
	      {
		switch  (fscanf(image->fptr, "%u ", &value ) )
		  {
		  case '1':
		    break ;
		  case EOF:
		    snprintf(syslog_message,message_size,"read_line(): %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(): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (have_oldline)
		      free (oldline);
		    exit (1);
		    break;
		  }
		if(value <= 255)
		  line[i] = line[i + 1] = line[i + 2] = value ;
		else
		  {
		    snprintf(syslog_message,message_size,"read_line(): %s", 
			     gMessages[E_IM_BADIMAGE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (have_oldline)
		      free (oldline);
		    exit (1);
		  }
	      }
	  }
	  break;
	case P3_PPM:
	  {
	    /* 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 ;
	    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(): %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(): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (have_oldline)
		      free (oldline);
		    exit (1);
		    break;
		  }
		if(value <= 255)
		  line[i] = value ;
		else
		  {
		    snprintf(syslog_message,message_size,"read_line() %s", 
			     gMessages[E_IM_BADIMAGE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		    free (line);
		    if (have_oldline)
		      free (oldline);
		    exit (1);
		  }
	      }
	  }
	  break;
	case P4_PBMRAW:
	  /* read pbmraw format and convert to ppmraw  format */
	  {
	      int pbm_width, count, index ;
	      char* pbmraw_data;
	      unsigned char mask[8] = 
	      { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 };
	      pbm_width = ( image->width + 7 ) / 8;
	      
	      if (! (pbmraw_data = malloc(pbm_width)) )
		{
		    snprintf(syslog_message,message_size,"read_line(): %s", 
			     gMessages[E_BADMALLOC]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		  if (have_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] = line[index + 1] = line[index + 2] = 0 ;
		     else
		       line[index] = line[index + 1] = line[index + 2] = 255; ;
		     index += 3;
		    }
		  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(): %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(): %s", 
			     gMessages[E_IM_BADREADLINE]);
		    wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		      free (line);
		      if (have_oldline)
			free (oldline);
		      exit (1);
		    }
	       }
	  }
	  
	  break;
	case P5_PGMRAW:
	  
	  /* read pgmraw format and convert to ppmraw  format */
	  {
	    int  index ;
	    char* pgm_data;
	    
	    pgm_data = malloc(image->width);
	    if (!pgm_data)
	      {
		snprintf(syslog_message,message_size,"read_line(): %s", 
			 gMessages[E_BADMALLOC]);
		wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		free (line);
		if (have_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] = line[index + 1] = line[index + 2]
		      = pgm_data[i] ;
		    index += 3;
		  }
		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:() %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(): %s", 
			      gMessages[E_IM_BADREADLINE]);
		     wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		     free (line);
		     if (have_oldline)
		       free (oldline);
		     exit (1);
		   }
	       }
	  }
	  break;
	case P6_PPMRAW:
	  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(): %s PPMRAW\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(): %s", 
			   gMessages[E_IM_BADREADLINE]);
		  wrap_syslog (LOG_CRIT,"%s",syslog_message); 
		  free (line);
		  if (have_oldline)
		    free (oldline);
		  exit (1);
		}
	    }
	  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 (have_oldline)
	    free (oldline);
	  exit (1);
	  break;
	}	
    }
  else  if (gGammaMode) 
    ProduceGamma (image, line ) ;  /* generate color calibration page */
  
  if ( gTerminate )
    {
      /* this 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
       */
      int index ;
      index = 0 ;
      for ( i = 0 ; i < image->width ;  i ++  )
	{
	  line[index] = line[index + 1] = line[index + 2] = 255; ;
	  index += 3;
	}
    }


  
  /* process ALL lines to avoid artifacts 
     (odd lines are only processed for black in *_Dither() functions )
     hopefully, this minimal processing is quite fast */
  
  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  )
	{
	  convert_black_data (image, line, &black[0][b_off]);
 	}
      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 * 3);
	      memcpy(oldline, line, image->width * 3 );
	      have_oldline = 1;
	    }
	}
      
      
    }
  else
    {
      /* return both black and color data on even lines */
      /* note that convert_color_data must be called AFTER convert_black_data */
      if ( have_oldline )
	{
	  /* add any black from previous unprinted odd line 
	     to next even line */
	  for ( i = 0; i < image->width * 3; i += 3 )
	    {
	      if ( !oldline[i] && !oldline[i + 1] && !oldline[i + 2] )
		{
		  line[i] = line[i + 1] = line[i + 2] = 0 ;
		}
	    }
	  free(oldline);  
	  have_oldline = 0;
	}
      
      
      
      convert_black_data (image, line, &black[0][b_off]);
      if (gColorMode)
	convert_color_data (image, line, color, c_off);
    }
  
  
  free (line);
  
  image->bufferCurLine++;
  
}

/* 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;

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

    remallocBuffer (image, linesize * lines);

    /* read_line into buffer and 'data' */
    read_line (image, image->buffer,
	       image->bufpos + image->buflines * linesize, data, offset);
    image->buflines++;

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

    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;	

    /* 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);
    image->buflines++;

    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));	//mocho changes /2
	image->bufpos += 3 * ((image->width / 2 + 7) / 8);	//mocho changes /2
	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--;
}