summaryrefslogtreecommitdiff
path: root/filter/raster.c
blob: 45bc06f849ee75e6f9743b5da52ce37587f8c82a (plain)
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
/*
 * "$Id$"
 *
 *   Raster file routines for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 1997-2006 by Easy Software Products.
 *
 *   This file is part of the CUPS Imaging library.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Easy Software Products and are protected by Federal
 *   copyright law.  Distribution and use rights are outlined in the file
 *   "LICENSE.txt" which should have been included with this file.  If this
 *   file is missing or damaged please contact Easy Software Products
 *   at:
 *
 *       Attn: CUPS Licensing Information
 *       Easy Software Products
 *       44141 Airport View Drive, Suite 204
 *       Hollywood, Maryland 20636 USA
 *
 *       Voice: (301) 373-9600
 *       EMail: cups-info@cups.org
 *         WWW: http://www.cups.org
 *
 *   This code and any derivative of it may be used and distributed
 *   freely under the terms of the GNU General Public License when
 *   used with GNU/GPL Ghostscript or its derivatives.  Use of the
 *   code (or any derivative of it) with software other than GNU/GPL
 *   GhostScript (or its derivatives) is governed by the CUPS license
 *   agreement.
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *   cupsRasterClose()        - Close a raster stream.
 *   cupsRasterOpen()         - Open a raster stream.
 *   cupsRasterReadHeader()   - Read a raster page header and store it in a
 *                              V1 page header structure.
 *   cupsRasterReadHeader2()  - Read a raster page header and store it in a
 *                              V2 page header structure.
 *   cupsRasterReadPixels()   - Read raster pixels.
 *   cupsRasterWriteHeader()  - Write a raster page header from a V1 page
 *                              header structure.
 *   cupsRasterWriteHeader2() - Write a raster page header from a V2 page
 *                              header structure.
 *   cupsRasterWritePixels()  - Write raster pixels.
 *   cups_raster_update()     - Update the raster header and row count for the
 *                              current page.
 *   cups_raster_write()      - Write a row of raster data...
 *   cups_read()              - Read bytes from a file.
 *   cups_write()             - Write bytes to a file.
 */

/*
 * Include necessary headers...
 */

#include "raster.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/string.h>

#if defined(WIN32) || defined(__EMX__)
#  include <io.h>
#else
#  include <unistd.h>
#endif /* WIN32 || __EMX__ */


/*
 * Private structures...
 */

struct _cups_raster_s			/**** Raster stream data ****/
{
  unsigned		sync;		/* Sync word from start of stream */
  int			fd;		/* File descriptor */
  cups_mode_t		mode;		/* Read/write mode */
  cups_page_header2_t	header;		/* Raster header for current page */
  int			count,		/* Current row run-length count */
			remaining,	/* Remaining rows in page image */
			bpp;		/* Bytes per pixel/color */
  unsigned char		*pixels,	/* Pixels for current row */
			*pend,		/* End of pixel buffer */
			*pcurrent;	/* Current byte in pixel buffer */
};

/*
 * Local functions...
 */

static unsigned	cups_raster_read_header(cups_raster_t *r);
static void	cups_raster_update(cups_raster_t *r);
static int	cups_raster_write(cups_raster_t *r);
static int	cups_read(int fd, unsigned char *buf, int bytes);
static int	cups_write(int fd, const unsigned char *buf, int bytes);


/*
 * 'cupsRasterClose()' - Close a raster stream.
 */

void
cupsRasterClose(cups_raster_t *r)	/* I - Stream to close */
{
  if (r != NULL)
  {
    if (r->pixels)
      free(r->pixels);

    free(r);
  }
}


/*
 * 'cupsRasterOpen()' - Open a raster stream.
 */

cups_raster_t *				/* O - New stream */
cupsRasterOpen(int         fd,		/* I - File descriptor */
               cups_mode_t mode)	/* I - Mode */
{
  cups_raster_t	*r;			/* New stream */


  if ((r = calloc(sizeof(cups_raster_t), 1)) == NULL)
    return (NULL);

  r->fd   = fd;
  r->mode = mode;

  if (mode == CUPS_RASTER_READ)
  {
   /*
    * Open for read - get sync word...
    */

    if (cups_read(r->fd, (unsigned char *)&(r->sync), sizeof(r->sync))
            < sizeof(r->sync))
    {
      free(r);
      return (NULL);
    }

    if (r->sync != CUPS_RASTER_SYNC &&
        r->sync != CUPS_RASTER_REVSYNC &&
        r->sync != CUPS_RASTER_SYNCv1 &&
        r->sync != CUPS_RASTER_REVSYNCv1)
    {
      free(r);
      return (NULL);
    }
  }
  else
  {
   /*
    * Open for write - put sync word...
    */

    r->sync = CUPS_RASTER_SYNC;
    if (cups_write(r->fd, (unsigned char *)&(r->sync), sizeof(r->sync))
            < sizeof(r->sync))
    {
      free(r);
      return (NULL);
    }
  }

  return (r);
}


/*
 * 'cupsRasterReadHeader()' - Read a raster page header and store it in a
 *                            V1 page header structure.
 */

unsigned				/* O - 1 on success, 0 on fail */
cupsRasterReadHeader(
    cups_raster_t      *r,		/* I - Raster stream */
    cups_page_header_t *h)		/* I - Pointer to header data */
{
 /*
  * Get the raster header...
  */

  if (!cups_raster_read_header(r))
    return (0);
  
 /*
  * Copy the header to the user-supplied buffer...
  */

  memcpy(h, &(r->header), sizeof(cups_page_header_t));

  return (1);
}


/*
 * 'cupsRasterReadHeader2()' - Read a raster page header and store it in a
 *                             V2 page header structure.
 *
 * @since CUPS 1.2@
 */

unsigned				/* O - 1 on success, 0 on fail */
cupsRasterReadHeader2(
    cups_raster_t       *r,		/* I - Raster stream */
    cups_page_header2_t *h)		/* I - Pointer to header data */
{
 /*
  * Get the raster header...
  */

  if (!cups_raster_read_header(r))
    return (0);
  
 /*
  * Copy the header to the user-supplied buffer...
  */

  memcpy(h, &(r->header), sizeof(cups_page_header2_t));

  return (1);
}


/*
 * 'cupsRasterReadPixels()' - Read raster pixels.
 */

unsigned				/* O - Number of bytes read */
cupsRasterReadPixels(cups_raster_t *r,	/* I - Raster stream */
                     unsigned char *p,	/* I - Pointer to pixel buffer */
		     unsigned      len)	/* I - Number of bytes to read */
{
  int		bytes;			/* Bytes read */
  unsigned	remaining;		/* Bytes remaining */
  unsigned char	*ptr,			/* Pointer to read buffer */
		byte;			/* Byte from file */


  if (r == NULL || r->mode != CUPS_RASTER_READ || r->remaining == 0)
    return (0);

  remaining = len;

  while (remaining > 0 && r->remaining > 0)
  {
    if (r->count == 0)
    {
     /*
      * Need to read a new row...
      */

      if (remaining == r->header.cupsBytesPerLine)
	ptr = p;
      else
	ptr = r->pixels;

      if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
      {
       /*
	* Read without compression...
	*/

        if (cups_read(r->fd, ptr, r->header.cupsBytesPerLine) <
	        r->header.cupsBytesPerLine)
	  return (0);

        r->count = 1;
      }
      else
      {
       /*
        * Read using a modified TIFF "packbits" compression...
	*/

        unsigned char	*temp;		/* Pointer into buffer */
	int		count;		/* Repetition count */


        if (cups_read(r->fd, &byte, 1) < 1)
	  return (0);

        r->count = byte + 1;

        if (r->count > 1)
	  ptr = r->pixels;

        temp  = ptr;
	bytes = r->header.cupsBytesPerLine;

	while (bytes > 0)
	{
	 /*
	  * Get a new repeat count...
	  */

          if (cups_read(r->fd, &byte, 1) < 1)
	    return (0);

	  if (byte & 128)
	  {
	   /*
	    * Copy N literal pixels...
	    */

	    count = (257 - byte) * r->bpp;

            if (count > bytes)
	      count = bytes;

            if (cups_read(r->fd, temp, count) < count)
	      return (0);

	    temp  += count;
	    bytes -= count;
	  }
	  else
	  {
	   /*
	    * Repeat the next N bytes...
	    */

            count = (byte + 1) * r->bpp;
            if (count > bytes)
	      count = bytes;

            if (count < r->bpp)
	      break;

	    bytes -= count;

            if (cups_read(r->fd, temp, r->bpp) < r->bpp)
	      return (0);

	    temp  += r->bpp;
	    count -= r->bpp;

	    while (count > 0)
	    {
	      memcpy(temp, temp - r->bpp, r->bpp);
	      temp  += r->bpp;
	      count -= r->bpp;
            }
	  }
	}
      }

      if ((r->header.cupsBitsPerColor == 16 ||
           r->header.cupsBitsPerPixel == 12 ||
           r->header.cupsBitsPerPixel == 16) &&
          (r->sync == CUPS_RASTER_REVSYNC || r->sync == CUPS_RASTER_REVSYNCv1))
      {
       /*
	* Swap bytes in the pixel data...
	*/

        unsigned char	*temp;
	int		count;


        for (temp = ptr, count = r->header.cupsBytesPerLine;
	     count > 0;
	     temp += 2, count -= 2)
	{
	  byte    = temp[0];
	  temp[0] = temp[1];
	  temp[1] = byte;
	}
      }

      if (remaining >= r->header.cupsBytesPerLine)
      {
	bytes       = r->header.cupsBytesPerLine;
        r->pcurrent = r->pixels;
	r->count --;
	r->remaining --;
      }
      else
      {
	bytes       = remaining;
        r->pcurrent = r->pixels + bytes;
      }

      if (ptr != p)
        memcpy(p, ptr, bytes);
    }
    else
    {
     /*
      * Copy fragment from buffer...
      */

      if ((bytes = r->pend - r->pcurrent) > remaining)
        bytes = remaining;

      memcpy(p, r->pcurrent, bytes);
      r->pcurrent += bytes;

      if (r->pcurrent >= r->pend)
      {
        r->pcurrent = r->pixels;
	r->count --;
	r->remaining --;
      }
    }

    remaining -= bytes;
    p         += bytes;
  }

  return (len);
}


/*
 * 'cupsRasterWriteHeader()' - Write a raster page header from a V1 page
 *                             header structure.
 */
 
unsigned				/* O - 1 on success, 0 on failure */
cupsRasterWriteHeader(
    cups_raster_t      *r,		/* I - Raster stream */
    cups_page_header_t *h)		/* I - Raster page header */
{
  if (r == NULL || r->mode != CUPS_RASTER_WRITE)
    return (0);

 /*
  * Make a copy of the header, and compute the number of raster
  * lines in the page image...
  */

  memset(&(r->header), 0, sizeof(r->header));
  memcpy(&(r->header), h, sizeof(cups_page_header_t));

  cups_raster_update(r);

 /*
  * Write the raster header...
  */

  return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
              > 0);
}


/*
 * 'cupsRasterWriteHeader2()' - Write a raster page header from a V2 page
 *                              header structure.
 *
 * @since CUPS 1.2@
 */
 
unsigned				/* O - 1 on success, 0 on failure */
cupsRasterWriteHeader2(
    cups_raster_t       *r,		/* I - Raster stream */
    cups_page_header2_t *h)		/* I - Raster page header */
{
  if (r == NULL || r->mode != CUPS_RASTER_WRITE)
    return (0);

 /*
  * Make a copy of the header, and compute the number of raster
  * lines in the page image...
  */

  memcpy(&(r->header), h, sizeof(cups_page_header2_t));

  cups_raster_update(r);

 /*
  * Write the raster header...
  */

  return (cups_write(r->fd, (unsigned char *)&(r->header), sizeof(r->header))
              > 0);
}


/*
 * 'cupsRasterWritePixels()' - Write raster pixels.
 */

unsigned				/* O - Number of bytes written */
cupsRasterWritePixels(cups_raster_t *r,	/* I - Raster stream */
                      unsigned char *p,	/* I - Bytes to write */
		      unsigned      len)/* I - Number of bytes to write */
{
  int		bytes;			/* Bytes read */
  unsigned	remaining;		/* Bytes remaining */


#ifdef DEBUG
  fprintf(stderr, "cupsRasterWritePixels(r=%p, p=%p, len=%u), remaining=%u\n",
          r, p, len, r->remaining);
#endif /* DEBUG */

  if (r == NULL || r->mode != CUPS_RASTER_WRITE || r->remaining == 0)
    return (0);

  for (remaining = len; remaining > 0; remaining -= bytes, p += bytes)

  {
   /*
    * Figure out the number of remaining bytes on the current line...
    */

    if ((bytes = remaining) > (r->pend - r->pcurrent))
      bytes = r->pend - r->pcurrent;

    if (r->count > 0)
    {
     /*
      * Check to see if this line is the same as the previous line...
      */

      if (memcmp(p, r->pcurrent, bytes))
      {
        if (!cups_raster_write(r))
	  return (0);

	r->count = 0;
      }
      else
      {
       /*
        * Mark more bytes as the same...
	*/

        r->pcurrent += bytes;

	if (r->pcurrent >= r->pend)
	{
	 /*
          * Increase the repeat count...
	  */

	  r->count ++;
	  r->pcurrent = r->pixels;

	 /*
          * Flush out this line if it is the last one...
	  */

	  r->remaining --;

	  if (r->remaining == 0)
	    return (cups_raster_write(r));
	  else if (r->count == 256)
	  {
	    if (cups_raster_write(r) == 0)
	      return (0);

	    r->count = 0;
	  }
	}

	continue;
      }
    }

    if (r->count == 0)
    {
     /*
      * Copy the raster data to the buffer...
      */

      memcpy(r->pcurrent, p, bytes);

      r->pcurrent += bytes;

      if (r->pcurrent >= r->pend)
      {
       /*
        * Increase the repeat count...
	*/

	r->count ++;
	r->pcurrent = r->pixels;

       /*
        * Flush out this line if it is the last one...
	*/

	r->remaining --;

	if (r->remaining == 0)
	  return (cups_raster_write(r));
      }
    }
  }

  return (len);
}


/*
 * 'cups_raster_read_header()' - Read a raster page header.
 */

static unsigned				/* O - 1 on success, 0 on fail */
cups_raster_read_header(
    cups_raster_t *r)			/* I - Raster stream */
{
  int		len;			/* Number of words to swap */
  union swap_s				/* Swapping structure */
  {
    unsigned char	b[4];
    unsigned		v;
  }		*s;


  if (r == NULL || r->mode != CUPS_RASTER_READ)
    return (0);

 /*
  * Get the length of the raster header...
  */

  if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1)
    len = sizeof(cups_page_header_t);
  else
    len = sizeof(cups_page_header2_t);

 /*
  * Read the header...
  */

  memset(&(r->header), 0, sizeof(r->header));

  if (cups_read(r->fd, (unsigned char *)&(r->header), len) < len)
    return (0);

 /*
  * Swap bytes as needed...
  */

  if (r->sync == CUPS_RASTER_REVSYNC || r->sync == CUPS_RASTER_REVSYNCv1)
    for (len = 81, s = (union swap_s *)&(r->header.AdvanceDistance);
	 len > 0;
	 len --, s ++)
      s->v = (((((s->b[3] << 8) | s->b[2]) << 8) | s->b[1]) << 8) | s->b[0];

 /*
  * Update the header and row count...
  */

  cups_raster_update(r);

  return (1);
}


/*
 * 'cups_raster_update()' - Update the raster header and row count for the
 *                          current page.
 */

static void
cups_raster_update(cups_raster_t *r)	/* I - Raster stream */
{
  if (r->sync == CUPS_RASTER_SYNCv1 || r->sync == CUPS_RASTER_REVSYNCv1 ||
      r->header.cupsNumColors == 0)
  {
   /*
    * Set the "cupsNumColors" field according to the colorspace...
    */

    switch (r->header.cupsColorSpace)
    {
      case CUPS_CSPACE_W :
      case CUPS_CSPACE_K :
      case CUPS_CSPACE_WHITE :
      case CUPS_CSPACE_GOLD :
      case CUPS_CSPACE_SILVER :
          r->header.cupsNumColors = 1;
	  break;

      case CUPS_CSPACE_RGB :
      case CUPS_CSPACE_CMY :
      case CUPS_CSPACE_YMC :
      case CUPS_CSPACE_CIEXYZ :
      case CUPS_CSPACE_CIELab :
      case CUPS_CSPACE_ICC1 :
      case CUPS_CSPACE_ICC2 :
      case CUPS_CSPACE_ICC3 :
      case CUPS_CSPACE_ICC4 :
      case CUPS_CSPACE_ICC5 :
      case CUPS_CSPACE_ICC6 :
      case CUPS_CSPACE_ICC7 :
      case CUPS_CSPACE_ICC8 :
      case CUPS_CSPACE_ICC9 :
      case CUPS_CSPACE_ICCA :
      case CUPS_CSPACE_ICCB :
      case CUPS_CSPACE_ICCC :
      case CUPS_CSPACE_ICCD :
      case CUPS_CSPACE_ICCE :
      case CUPS_CSPACE_ICCF :
          r->header.cupsNumColors = 3;
	  break;

      case CUPS_CSPACE_RGBA :
      case CUPS_CSPACE_RGBW :
      case CUPS_CSPACE_CMYK :
      case CUPS_CSPACE_YMCK :
      case CUPS_CSPACE_KCMY :
      case CUPS_CSPACE_GMCK :
      case CUPS_CSPACE_GMCS :
          r->header.cupsNumColors = 4;
	  break;

      case CUPS_CSPACE_KCMYcm :
          if (r->header.cupsBitsPerPixel < 8)
            r->header.cupsNumColors = 6;
	  else
            r->header.cupsNumColors = 4;
	  break;
    }
  }

 /*
  * Set the number of bytes per pixel/color...
  */

  if (r->header.cupsColorOrder == CUPS_ORDER_CHUNKED)
    r->bpp = (r->header.cupsBitsPerPixel + 7) / 8;
  else
    r->bpp = (r->header.cupsBitsPerColor + 7) / 8;

 /*
  * Set the number of remaining rows...
  */

  if (r->header.cupsColorOrder == CUPS_ORDER_PLANAR)
    r->remaining = r->header.cupsHeight * r->header.cupsNumColors;
  else
    r->remaining = r->header.cupsHeight;

 /*
  * Allocate the read/write buffer...
  */

  if (r->pixels != NULL)
    free(r->pixels);

  r->pixels   = calloc(r->header.cupsBytesPerLine, 1);
  r->pcurrent = r->pixels;
  r->pend     = r->pixels + r->header.cupsBytesPerLine;
  r->count    = 0;
}


/*
 * 'cups_raster_write()' - Write a row of raster data...
 */

static int				/* O - Number of bytes written */
cups_raster_write(cups_raster_t *r)	/* I - Raster stream */
{
  unsigned char	*start,			/* Start of sequence */
		*ptr,			/* Current pointer in sequence */
		byte;			/* Byte to write */
  int		count;			/* Count */


#ifdef DEBUG
  fprintf(stderr, "cups_raster_write(r=%p)\n", r);
#endif /* DEBUG */

 /*
  * Write the row repeat count...
  */

  byte = r->count - 1;

  if (cups_write(r->fd, &byte, 1) < 1)
  {
#ifdef DEBUG
    fputs("cups_raster_write: Unable to write row repeat count...\n",
          stderr);
#endif /* DEBUG */

    return (0);
  }

 /*
  * Write using a modified TIFF "packbits" compression...
  */

  for (ptr = r->pixels; ptr < r->pend;)
  {
    start = ptr;
    ptr += r->bpp;

    if (ptr == r->pend)
    {
     /*
      * Encode a single pixel at the end...
      */

      byte = 0;
      if (cups_write(r->fd, &byte, 1) < 1)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write last pixel count...\n", stderr);
#endif /* DEBUG */

        return (0);
      }

      if (cups_write(r->fd, start, r->bpp) < r->bpp)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write last pixel data...\n", stderr);
#endif /* DEBUG */

        return (0);
      }
    }
    else if (!memcmp(start, ptr, r->bpp))
    {
     /*
      * Encode a sequence of repeating pixels...
      */

      for (count = 2; count < 128 && ptr < (r->pend - r->bpp); count ++, ptr += r->bpp)
        if (memcmp(ptr, ptr + r->bpp, r->bpp) != 0)
	  break;

      ptr += r->bpp;

      byte = count - 1;

      if (cups_write(r->fd, &byte, 1) < 1)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write repeated pixel count...\n", stderr);
#endif /* DEBUG */

        return (0);
      }

      if (cups_write(r->fd, start, r->bpp) < r->bpp)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write repeated pixel data...\n", stderr);
#endif /* DEBUG */

        return (0);
      }
    }
    else
    {
     /*
      * Encode a sequence of non-repeating pixels...
      */

      for (count = 1; count < 127 && ptr < (r->pend - r->bpp); count ++, ptr += r->bpp)
        if (!memcmp(ptr, ptr + r->bpp, r->bpp))
	  break;

      if (ptr >= (r->pend - r->bpp) && count < 128)
      {
        count ++;
	ptr += r->bpp;
      }
 
      byte = 257 - count;

      if (cups_write(r->fd, &byte, 1) < 1)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write non-repeating pixel count...\n", stderr);
#endif /* DEBUG */

        return (0);
      }

      count *= r->bpp;

      if (cups_write(r->fd, start, count) < count)
      {
#ifdef DEBUG
        fputs("cups_raster_write: Unable to write non-repeating pixel data...\n", stderr);
#endif /* DEBUG */

        return (0);
      }
    }
  }

  return (r->header.cupsBytesPerLine);
}


/*
 * 'cups_read()' - Read bytes from a file.
 */

static int				/* O - Bytes read or -1 */
cups_read(int           fd,		/* I - File descriptor */
          unsigned char *buf,		/* I - Buffer for read */
	  int           bytes)		/* I - Number of bytes to read */
{
  int	count,				/* Number of bytes read */
	total;				/* Total bytes read */


  for (total = 0; total < bytes; total += count, buf += count)
  {
    count = read(fd, buf, bytes - total);

    if (count == 0)
      return (0);
    else if (count < 0)
    {
      if (errno == EINTR)
        count = 0;
      else
        return (-1);
    }
  }

  return (total);
}


/*
 * 'cups_write()' - Write bytes to a file.
 */

static int				/* O - Bytes written or -1 */
cups_write(int                 fd,	/* I - File descriptor */
           const unsigned char *buf,	/* I - Bytes to write */
	   int                 bytes)	/* I - Number of bytes to write */
{
  int	count,				/* Number of bytes written */
	total;				/* Total bytes written */


  for (total = 0; total < bytes; total += count, buf += count)
  {
    count = write(fd, buf, bytes - total);

    if (count < 0)
    {
      if (errno == EINTR)
        count = 0;
      else
        return (-1);
    }
  }

  return (total);
}


/*
 * End of "$Id$".
 */