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
|
/*
*
* Kodak ESP Cxxx (OPL?) Control Language filter for the Common UNIX
* Printing System (CUPS).
*
* copyright Paul Newall May 2010 - Jan 2012. VERSION 2.4 (c2esp24)
* SUPPORT FOR ESP Cxxxx and Hero SERIES
* patch by user awl29 applied to fix problems with non bi-directional printers, smb shared
* data chunk size limit applied
* Params: job-id user title copies options [file]
* options = "noback" disables all calls to the back channel for testing.
*
This filter is based loosely on c2esp
*/
#define MAXDATACHUNK 65511 /* it seems windows smb printer can't handle more than 65K at a time? maybe others too */
#define DEBUGFILES 0 /* DEBUGFILES 1 creates files in /tmp to help debug
Currently a large number of files:
KodakPrintLog = text file showing progress of the filter
RasForComp.pbm or ppm = the raster read from cups raster view with image viewer.
KodakUncompressed = The binary page data before compression.
KodakCompPage = The zlib compressed page data
KodakPrintFile = The data that is sent to the printer
*/
#define TESTING 0 /* TESTING 1 suppresses the output to the printer. Used in development. */
#include <cups/raster.h>
#include <cups/sidechannel.h> //FlushBackChannel, and the side channel functions and constants
#include <fcntl.h> //files
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <time.h> //time functions used for debugging
#include "c2espcommon.h" //the common library of c2esp and c2espC
/* for gzip */
#include "zlib.h"
#define SET_BINARY_MODE(file)
#define CHUNK 16384
/*
* Constants...
*/
char *Version = "c2espC24";
unsigned char ESC = 255;
/*
* Globals...
*/
unsigned char *CupsLineBuffer; //buffer for one line of the cups raster
unsigned char *DataLineBuffer; //buffer for one uncompressed line
char KodakPaperSize[50]; /* String that the printer expects for paper size */
int OutBitsPerPixel, /* Number of bits per color per pixel for printer*/
Duplex, /* Current duplex mode */
Page, /* Current page number */
Canceled, /* Has the current job been canceled? */
DoBack; /* Enables the back channel comms */
int MemUsed = 0; //tracks memory use
time_t TimeStart; //to record the start of a section
FILE *PrintFile = NULL; //file descriptor for debug file
FILE *UncompressedFile = NULL; //file descriptor for file of uncompressed page data
time_t StartTime;
time_t KeepAwakeStart;
//for zlib
z_stream strm;
unsigned char out[CHUNK]; /*buffer for a compressed line */
#if DEBUGFILES == 1
FILE *dfp = NULL; //file descriptor for composite raster file
FILE *Cyanfp = NULL; //file descriptor for cyan only raster file
FILE *Magentafp = NULL; //file descriptor for magenta only raster file
FILE *Yellowfp = NULL; //file descriptor for yellow only raster file
FILE *Blackfp = NULL; //file descriptor for black only raster file
FILE *RawColourFile = NULL; //file descriptor for input to dither
FILE *DitheredColourFile = NULL; //file descriptor for output from dither
#endif
time_t KeepAwake(time_t Start, int Interval)
{
// Keeps the printer connection awake by sending DeviceStatus query not sooner than the specified interval in seconds
// Usage: Start = KeepAwake(Start, Interval);
if(time(NULL) - Start > Interval)
{
DoLog("Keeping printer awake by DeviceStatus?\n",0,0);
GoodExchange(PrintFile, "DeviceStatus?", "0101,DeviceStatus.ImageDevice", DoBack, 1, 1.0);
return (time(NULL));
}
else return (Start);
}
void
SetupPrinter(cups_page_header2_t *header)
{
//gets the printer ready to start the job
int i;
for(i=0; i<4; ++i)
{
if(GoodExchange(PrintFile, "LockPrinterWait?", "0002, OK, Locked for printing;", DoBack, 1, 3.0) >= 0) break;
}
DoOutJob(PrintFile, "Event=StartOfJob;",0,0); //printer command
if (DoBack) {
GoodExchange(PrintFile, "DeviceStatus?", "0101,DeviceStatus.ImageDevice", DoBack, 1, 1.0);
// you can get unexpected reply if there is an ink low warning then GoodExchange will be -ve
//aquire ink levels here? DeviceStatus.Printer.InkLevelPercent.Colour=nn%&DeviceStatus.Printer.InkLevelPercent.Black=nn%
//note & used as separator
DoLog("ColourPercent=%d\n",ColourPercent,0);
DoLog("BlackPercent=%d\n",BlackPercent,0);
if(ColourPercent >= 0 && BlackPercent >= 0)
{
fprintf(stderr,"ATTR: marker-levels=%d,%d\n",BlackPercent,ColourPercent); // sets the levels displayed
}
}
DoOutJob(PrintFile, KodakPaperSize,0,0);
if(header->MediaPosition == 0) DoOutJob(PrintFile, "MediaInputTrayCheck=Main;",0,0);
else if(header->MediaPosition == 1) DoOutJob(PrintFile, "MediaInputTrayCheck=Photo;",0,0);
else
{
DoOutJob(PrintFile, "MediaInputTrayCheck=Main;",0,0);
DoLog("Unknown Input Tray no. %d so used main tray", header->MediaPosition, 0);
}
if (DoBack) {
GoodExchange(PrintFile, "MediaTypeStatus?", "MediaTypeStatus=custom-media-type-deviceunavailable", DoBack, 1, 1.0);
GoodExchange(PrintFile, "MediaDetect?", "0098, OK, Media Detect Started;", DoBack, 1, 1.0);
//do MediaTypeStatus? until some media is found typically takes 12 seconds for Hero 9.1
#if TESTING == 0
sleep(5);
for(i=0; i<15; ++i) //normal
#endif
#if TESTING == 1
for(i=0; i<2; ++i) //short for tests
#endif
{
DoLog("MediaTypeStatus? try %d\n", i, 0);
if(GoodExchange(PrintFile, "MediaTypeStatus?",
"MediaTypeStatus=custom-media-type-deviceunavailable", DoBack, 2, 2.0) <= 0) break;
}
}
else {
GoodExchange(PrintFile, "MediaDetect?", "0098, OK, Media Detect Started;", DoBack, 1, 1.0);
sleep(12);
}
}
void
ShutdownPrinter(void)
{
int i, ret;
DoOutJob(PrintFile, "Event=EndOfJob;",0,0);
for(i=0; i<20; ++i) /* fast PC might need lots of tries here for printer to finish, how many is reasonable? */
{
/* First few tries will be quick so small jobs finish quickly */
if(i<5) ret=GoodExchange(PrintFile, "UnlockPrinter?", "0003, OK, Printer unlocked;", DoBack, 1, 5.0);
/* Then tries will be slower so long jobs can finish */
else ret=GoodExchange(PrintFile, "UnlockPrinter?", "0003, OK, Printer unlocked;", DoBack, 5, 2.0);
DoLog("UnlockPrinter? try %d returned %d\n", i, ret);
if(ret >= 0 || strcmp(BackBuf , "3405, Error, Printer not locked") == 0) break;
//error string has no terminating ; because it has been tokenised
}
}
void
SetupJob(cups_page_header2_t *header) //Prepare the printer for printing a job.
{
DoOutJob(PrintFile, "OutputBin=MainSink;",0,0);
Duplex = header->Duplex;
if(Duplex == 0) DoOutJob(PrintFile, "Sides=OneSided;",0,0);
else DoOutJob(PrintFile, "Sides=TwoSided;",0,0);
//DoOutJob(PrintFile, "PageOrder=BackToFront;",0,0); //may be possible to implement this
//DoOutJob(PrintFile, "Copies=1;",0,0); //may an error response? but is sent by windows
DoOutJob(PrintFile, "MediaType=custom-media-type-autoselection-0-0-0-0;",0,0);
DoOutJob(PrintFile, KodakPaperSize,0,0);
}
void
FreeBuffers()
{
DoLog("Free buffers\n",0,0);
free(CupsLineBuffer);
free(DataLineBuffer);
MemUsed = 0;
DoLog("Buffers freed\n",0,0);
}
void
AllocateBuffers(cups_page_header2_t *header)
{
// Allocate memory for a page of graphics...
if ((CupsLineBuffer = malloc(header->cupsBytesPerLine)) == NULL)
{
DoLog("ERROR: Unable to allocate %d bytes for CupsLineBuffer!\n",header->cupsBytesPerLine,0);
exit(1);
}
else MemUsed = MemUsed + header->cupsBytesPerLine * 1E-3;
if ((DataLineBuffer = malloc(header->cupsBytesPerLine+12)) == NULL)
{
DoLog("ERROR: Unable to allocate %d bytes for DataLineBuffer!\n",header->cupsBytesPerLine+8,0);
exit(1);
}
else MemUsed = MemUsed + (header->cupsBytesPerLine+12) * 1E-3;
DoLog("Buffers allocated %d kb\n",MemUsed,0);
}
void
StartPrinterPage(cups_page_header2_t *header)
{
int ResX, ResY;
DisplayHeader(header);
ResX = header->HWResolution[0];
ResY = header->HWResolution[1];
DoOutJob(PrintFile, "Event=StartOfPage;",0,0);
DoOutJob(PrintFile, "Page=%d;",Page,0);
DoOutJob(PrintFile, "Origin.Top=0.0mm;Origin.Left=0.0mm;",0,0);
// DoOutJob(PrintFile, "Origin.Top=1.0mm;Origin.Left=1.0mm;",0,0);
if(ResX==300)
{
DoOutJob(PrintFile, "PrintQuality=Draft;",0,0);
}
else if(ResX==600)
{
DoOutJob(PrintFile, "PrintQuality=Normal;",0,0);
}
else if(ResX==1200) // may not be an option
{
DoOutJob(PrintFile, "PrintQuality=High;",0,0);
}
if (header->cupsColorSpace == CUPS_CSPACE_CMY)
{
DoLog("CUPS_CSPACE_CMY (%d)\n",header->cupsColorSpace,0);
DoOutJob(PrintFile, "PrintColorspace=Color;",0,0);
DoOutJob(PrintFile, "AntiBleedControl=Auto;",0,0);
DoOutJob(PrintFile, "Resolution=%dx%d;", ResX, ResY);
DoOutJob(PrintFile, "RasterObject.BitsPerPixel=%d;",OutBitsPerPixel,0);
DoOutJob(PrintFile, "RasterObject.Colorspace=sRGB;",0,0);
}
else if (header->cupsColorSpace == CUPS_CSPACE_K)
{
DoLog("CUPS_CSPACE_K (%d)\n",header->cupsColorSpace,0);
DoOutJob(PrintFile, "PrintColorspace=Grayscale;",0,0);
DoOutJob(PrintFile, "AntiBleedControl=Off;",0,0);
DoOutJob(PrintFile, "Resolution=%dx%d;", ResX, ResY);
DoOutJob(PrintFile, "RasterObject.BitsPerPixel=%d;",OutBitsPerPixel,0);
DoOutJob(PrintFile, "RasterObject.Colorspace=Mono;",0,0);
}
else
{
DoLog("CUPS_CSPACE_?? (%d)\n",header->cupsColorSpace,0);
}
DoOutJob(PrintFile, "RasterObject.Compression=GZIPTok;",0,0);
DoOutJob(PrintFile, "RasterObject.Width=%d;", header->cupsWidth,0);
DoOutJob(PrintFile, "RasterObject.Height=%d;", header->cupsHeight,0);
}
void
EndPage(void) //Finish a page of graphics.
{
DoOutJob(PrintFile, "Event=EndOfPage;", 0,0);
if(PrintFile) fflush(PrintFile);
FreeBuffers(); /* Free memory... allocated by AllocateBuffers() */
}
void
CancelJob(int sig) /* - Cancel the current job... I - Signal */
{
(void)sig;
DoLog("CancelJob: job cancelled by signal\n",0,0);
CloseLogging();
Canceled = 1;
}
unsigned char Byte0(int In)
{
int Byte0Mask = 0xFF;
return (In & Byte0Mask);
}
unsigned char Byte1(int In)
{
int Byte0Mask = 0xFF;
return ((In>>8) & Byte0Mask);
}
int Composite(unsigned char Buffer[],int Posn)
{
//returns the sum of the 3 colour components corresponding to Posn, assuming chunked colour order
return (Buffer[Posn*3]+Buffer[Posn*3+1]+Buffer[Posn*3+2]);
}
/*
* 'main()' - Main entry and processing of driver.
*/
int /* O - Exit status */
main(int argc, char *argv[]) /* I - Number of command-line arguments, Command-line arguments */
{
int fd; /* File descriptor */
cups_raster_t *ras; /* Raster stream from cups */
cups_page_header2_t header; /* Page header from cups */
int y, c;
int i;
int PixelsLeft,PixelsRight; //count blank pixels at right and left of raster line
int PixelsNonBlank; //in a raster line
int DataLength; //length of an uncompressed data line
unsigned char MinOut=255, MaxOut=0; //to check the range of the dithered output or raster
int CheckCount, ret, have, flush;
FILE *CompData; //stores the compressed data of a page
char RasFileName[100]="";
int BytesToPrint;
int CurrentChunkSize;
StartTime = time(NULL);
KeepAwakeStart = time(NULL);
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action; /* Actions for POSIX signals */
#endif /* HAVE_SIGACTION && !HAVE_SIGSET */
/*
* Check command-line...
*/
if (argc < 6 || argc > 7) //wrong no of arguments
{
fprintf(stderr, ("Usage: %s job-id user title copies options [file]\n"), "rastertoek");
return (1);
}
char data;
int datalen;
cups_sc_bidi_t bidi;
cups_sc_status_t status;
/* Tell cupsSideChannelDoRequest() how big our buffer is... */
datalen = 1;
/* Get the bidirectional capabilities, waiting for up to 1 second */
status = cupsSideChannelDoRequest(CUPS_SC_CMD_GET_BIDI, &data, &datalen, 1.0);
/* Use the returned value if OK was returned and the length is still 1 */
if (status == CUPS_SC_STATUS_OK && datalen == 1) {
bidi = (cups_sc_bidi_t) data;
DoBack = (bidi == CUPS_SC_BIDI_NOT_SUPPORTED ? 0 : 1);
}
else {
bidi = CUPS_SC_BIDI_NOT_SUPPORTED;
DoBack = 0;
}
#if DEBUGFILES == 1
SetupLogging("c2espC",DoBack,"/tmp/KodakPrintLog");
#else
SetupLogging("c2espC",DoBack,"");
#endif
setbuf(stderr, NULL);
fprintf(stderr, ("DEBUG: ================= %s ===================================\n"),Version);
DoLogString("Starting %s\n",Version);
DoLog("Number of command line parameters: %d\n", argc, 0);
int argi;
for (argi = 0; argi < argc; argi++) {
DoLogString(" param: '%s'\n", argv[argi]);
}
DoLogString("End of command-line parameters\n", "");
DoLogString("Bi-di/backchannel support: %s\n", (bidi == CUPS_SC_BIDI_NOT_SUPPORTED ? "false" : "true"));
DoLog("DoBack value: %d\n", DoBack, 0);
MarkerSetup();
/*
* Open the page stream...
*/
if (argc == 7)
{
if ((fd = open(argv[6], O_RDONLY)) == -1)
{
fprintf(stderr, ("ERROR: c2espC: Unable to open raster file - %s\n"), strerror(errno));
sleep(1);
return (1);
}
}
else fd = 0;
fprintf(stderr, ("DEBUG: c2espC: opening raster\n"));
ras = cupsRasterOpen(fd, CUPS_RASTER_READ);
/*
* Register a signal handler to eject the current page if the
* job is cancelled.
*/
Canceled = 0;
#ifdef HAVE_SIGSET /* Use System V signals over POSIX to avoid bugs */
sigset(SIGTERM, CancelJob);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = CancelJob;
sigaction(SIGTERM, &action, NULL);
#else
signal(SIGTERM, CancelJob);
#endif /* HAVE_SIGSET */
/*
* Initialize the print device...
*/
#if DEBUGFILES == 1
PrintFile = fopen("/tmp/KodakPrintFile", "w");//open the print file
sleep(3); //does this help chmod to work?
chmod("/tmp/KodakPrintFile", S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
#endif
/* read the first header */
if(cupsRasterReadHeader2(ras, &header))
{
DoLog("First page Header read after %d sec\n", time(NULL)-StartTime,0);
SetPaperSize(KodakPaperSize, header.PageSize[1]);
SetupPrinter(&header);
DoLog("Printer should be ready by now\n",0,0);
/*
* Process pages as needed...
*/
Page = 0;
do //start of loop for each page
{
DoLog("Header read\n", 0,0);
if (Canceled) break;
if(header.cupsWidth<=0) break;
Page ++;
DoLog("PAGE %d COPIES %d\n", Page, header.NumCopies);
/*
* Start the page...
*/
if ( !(CompData=tmpfile()) )
{
perror("opening compressed page temp file");
abort();
}
OutBitsPerPixel = header.cupsBitsPerColor;
if (header.cupsColorSpace == CUPS_CSPACE_CMY) //colour
{
DoLog("cupsColorSpace = %d = CUPS_CSPACE_CMY\n", header.cupsColorSpace, 0);
fprintf(stderr, "INFO: c2espC: p%d Colour %d\n", Page, ColourPercent);
sprintf(RasFileName,"/tmp/RasForComp.ppm");
}
else if (header.cupsColorSpace == CUPS_CSPACE_K)//monochrome
{
DoLog("cupsColorSpace = %d = CUPS_CSPACE_K\n", header.cupsColorSpace, 0);
fprintf(stderr, "INFO: c2espC: p%d Monochrome\n",Page);
sprintf(RasFileName,"/tmp/RasForComp.pbm");
}
else
{
DoLog("Unknown cupsColorSpace = %d\n", header.cupsColorSpace, 0);
DoLog("Allowed cupsColorSpace = %d or %d\n", CUPS_CSPACE_CMY, CUPS_CSPACE_K);
}
DoLog("cups raster w = %d h = %d\n", header.cupsWidth, header.cupsHeight);
DoLog("cups raster bytes per line = %d bits per line = %d\n", header.cupsBytesPerLine,
header.cupsBytesPerLine * 8);
#if DEBUGFILES == 1
//open raster file here
fprintf(stderr, "INFO: c2espC: Opening %s\n",RasFileName);
remove(RasFileName);
dfp = fopen(RasFileName, "w");
UncompressedFile = fopen("/tmp/KodakUncompressed", "w"); //open the file
sleep(3); //does this help chmod to work?
chmod(RasFileName, S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
chmod("/tmp/KodakUncompressed", S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
if (dfp && header.cupsColorSpace == CUPS_CSPACE_CMY)
{
fprintf(dfp, "P6\n%8d %8d %8d\n", header.cupsWidth, header.cupsHeight, 255);
}
if (dfp && header.cupsColorSpace == CUPS_CSPACE_K)
{
fprintf(dfp, "P5\n%8d %8d %8d\n", header.cupsWidth, header.cupsHeight, 255);
}
#endif
AllocateBuffers(&header);
MinOut=255;MaxOut=0; //initialise
if(Page == 1) SetupJob(&header);
StartPrinterPage( &header);
//prepare for compression
DoLog("initialising Compression\n",0,0);
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, Z_DEFAULT_COMPRESSION);
if (ret != Z_OK)
{ //was return ret;
DoLog("Deflate did not initialise properly\n", 0, 0);
}
for (y = 0; (y < header.cupsHeight); ++y ) // Loop for each line of the page
{
if (Canceled) break;
/* Keep the printer connection awake, added if (DoBack) 15/1/12 */
if (DoBack) KeepAwakeStart = KeepAwake(KeepAwakeStart,10);
//read a line
if (!cupsRasterReadPixels(ras, CupsLineBuffer, header.cupsBytesPerLine)) break;
//check for max and min either colour or mono
for(i=0;i<(header.cupsBytesPerLine);++i)
{
if (CupsLineBuffer[i]>MaxOut) MaxOut=CupsLineBuffer[i];
if (CupsLineBuffer[i]<MinOut) MinOut=CupsLineBuffer[i];
}
//turn the line into print data
if (header.cupsColorSpace == CUPS_CSPACE_CMY) //colour - should be 3 x 8 bits per pixel
{
//count left blank pixels for colour - edit me
for(PixelsLeft=0;(Composite(&CupsLineBuffer[0], PixelsLeft)==0
&& PixelsLeft<header.cupsWidth);++PixelsLeft);
//count right blank pixels for colour - edit me
if(PixelsLeft==header.cupsWidth) PixelsRight=0;
else for(PixelsRight=0;Composite(&CupsLineBuffer[0],
header.cupsWidth-1-PixelsRight)==0 && (PixelsRight < header.cupsWidth);++PixelsRight);
} //end of colour section
else //monochrome - should be 8 bits per pixel
{
//count left blank pixels for mono
for(PixelsLeft=0;(CupsLineBuffer[PixelsLeft]==0
&& PixelsLeft<header.cupsWidth);++PixelsLeft);
//count right blank pixels for mono
if(PixelsLeft==header.cupsWidth) PixelsRight=0;
else for(PixelsRight=0;((CupsLineBuffer[header.cupsWidth-1-PixelsRight]==0)
&& (PixelsRight<header.cupsWidth));++PixelsRight);
} //end of mono
// common mono and colour
PixelsNonBlank=header.cupsWidth-PixelsLeft-PixelsRight;
//log the counts
//fprintf(LogFile,"%d+%d+%d=%d\n",PixelsLeft,PixelsNonBlank,PixelsRight,PixelsLeft+PixelsRight+PixelsNonBlank);
//generate the data
//LHS blanks - maybe should not do if PixelsLeft==0 ? need a test file
DataLineBuffer[0]=Byte0(PixelsLeft);
DataLineBuffer[1]=Byte1(PixelsLeft);
DataLineBuffer[2]=0x01;
DataLineBuffer[3]=0x00;
DataLength=4;
if(PixelsLeft<header.cupsWidth) //there are some non blank pixels
{
DataLineBuffer[4]=Byte0(PixelsNonBlank);
DataLineBuffer[5]=Byte1(PixelsNonBlank);
DataLineBuffer[6]=0x00;
DataLineBuffer[7]=0x00;
DataLength+=4;
if (header.cupsColorSpace == CUPS_CSPACE_CMY) //colour - should be 3 x 8 bits per pixel
{
for(i=0;i<PixelsNonBlank;++i)
{
DataLineBuffer[8+i]=CupsLineBuffer[(PixelsLeft+i)*3+0];
DataLineBuffer[8+PixelsNonBlank+i]=CupsLineBuffer[(PixelsLeft+i)*3+1];
DataLineBuffer[8+PixelsNonBlank*2+i]=CupsLineBuffer[(PixelsLeft+i)*3+2];
}
DataLength+=PixelsNonBlank*3;
}
else // its mono 1 x 8 bits per pixel
{
for(i=0;i<PixelsNonBlank;++i) DataLineBuffer[8+i]=CupsLineBuffer[PixelsLeft+i];
DataLength+=PixelsNonBlank;
}
// for colour and mono
if(PixelsRight>0) //there are some RHS blanks
{
DataLineBuffer[DataLength+0]=Byte0(PixelsRight);
DataLineBuffer[DataLength+1]=Byte1(PixelsRight);
DataLineBuffer[DataLength+2]=0x01;
DataLineBuffer[DataLength+3]=0x00;
DataLength+=4;
}
}
if(UncompressedFile) fwrite(DataLineBuffer, 1, DataLength, UncompressedFile);
//send data to the compression
strm.next_in = DataLineBuffer;
strm.avail_in = DataLength;
if(y >= header.cupsHeight-1) flush=Z_FINISH;
else flush = Z_NO_FLUSH;
/* run deflate() on input until output buffer not full */
do
{
strm.avail_out = CHUNK;
strm.next_out = out;
// DoLog("deflating %d byte input\n",strm.avail_in,0);
ret = deflate(&strm, flush);
if(ret == Z_STREAM_ERROR) break;
have = CHUNK - strm.avail_out;
if(have>0) DoLog("writing deflated output %d bytes\n",have,0);
if (fwrite(out, 1, have, CompData) != have || ferror(CompData))
{
(void)deflateEnd(&strm);
ret = Z_STREAM_ERROR; //kludge
break;
}
} while (strm.avail_out == 0);
if(ret == Z_STREAM_ERROR) break;
#if DEBUGFILES == 1
//store the raster for debugging - may need changing for colour
if (dfp) fwrite(CupsLineBuffer, 1, header.cupsBytesPerLine, dfp);
#endif
} //end of line loop
if(ret == Z_STREAM_ERROR)
{
DoLog("Compression error no %d\n",Z_ERRNO,0);
}
/* end of compression */
//put compressed byte count into output file and copy compressed data in
(void)deflateEnd(&strm);
DoLog("Compression finished\n",0,0);
DoLog("Rewinding compressed page file\n",0,0);
rewind(CompData);
/* Old version sent data all in one go. This seems to cause a problem with smb shared printers.
DoOutJob(PrintFile, "RasterObject.Data#%d=", strm.total_out,0);
CheckCount=0;
while((c=fgetc(CompData)) != EOF)
{
#if DEBUGFILES == 1
fputc(c,PrintFile);
#endif
#if TESTING == 0
fputc(c,stdout);
#endif
++CheckCount;
}
DoOutJob(PrintFile, ";",0,0); //one semi colon after the data
fflush(PrintFile);
*/
// New version with chunk size limit 15/1/12
CheckCount=0;
BytesToPrint = strm.total_out;
CurrentChunkSize = 0;
while (BytesToPrint > 0) {
if (BytesToPrint <= MAXDATACHUNK) {
CurrentChunkSize = BytesToPrint;
}
else {
CurrentChunkSize = MAXDATACHUNK;
}
//send one chunk of data
DoOutJob(PrintFile, "RasterObject.Data#%d=", CurrentChunkSize,0);
//if(cbarg != NULL) rc = fwrite(CurrentChunkStart, 1, CurrentChunkSize, cbarg);
DoLog("GZIP data chunk is sent to printer\n",0,0);
//rc = fwrite(CurrentChunkStart, 1, CurrentChunkSize, stdout); //also to output
for(i=0;i<CurrentChunkSize;++i)
{
c=fgetc(CompData);
#if DEBUGFILES == 1
fputc(c,PrintFile);
#endif
#if TESTING == 0
fputc(c,stdout);
#endif
++CheckCount;
}
DoOutJob(PrintFile, ";",0,0); //one semi colon after the chunk
BytesToPrint -= CurrentChunkSize;
}
DoLog("Compressed data copied to output expect %d read %d\n",strm.total_out,CheckCount);
DoLog("Page raster built at %d sec\n",time(NULL)-StartTime,0);
DoLog("Max and min in raster are %d %d\n",MaxOut,MinOut);
//page is finished
#if DEBUGFILES == 1
//close the debug files
if(UncompressedFile) fclose(UncompressedFile);
if(dfp) fclose(dfp);
#endif
EndPage();
if (Canceled) break;
}
while (cupsRasterReadHeader2(ras, &header));
}
else DoLog("no headers so nothing to print",0,0);
if (Page > 0) ShutdownPrinter();
/*
* Close the raster stream...
*/
cupsRasterClose(ras);
DoLog("cups raster closed after %d sec\n",time(NULL)-StartTime,0);
if (fd != 0) close(fd);
/*
* Termination, send an error message if required...
*/
DoLog("c2espC terminating after %d sec. Processed %d pages\n",time(NULL)-StartTime,Page);
if (Page == 0)
{
DoLog("ERROR: c2espC: No pages found!\n",0,0);
return (1);
}
else
{
CloseLogging();
if(PrintFile != NULL) fclose(PrintFile);
#if DEBUGFILES == 1
//let anyone read the files How much delay is needed if any?
// sleep(3); //does this help chmod to work?
chmod("/tmp/KodakCompPage", S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
chmod("/tmp/KodakPrintLog", S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
chmod("/tmp/KodakPrintFile", S_IRUSR | S_IWUSR | S_IROTH ); //let anyone read it
#endif
//cups seems to replace this by "Ready to print" so you don't see it
//fprintf(stderr, "INFO: Done - Blk %d Col %d percent\n",BlackPercent, ColourPercent);
fprintf(stderr, "INFO: Done\n");
return (0);
}
}
|