summaryrefslogtreecommitdiff
path: root/base/gdevmr8n.c
blob: c20bce43be3dde46783ef9f7533e26534005bc35 (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
/* Copyright (C) 2001-2023 Artifex Software, Inc.
   All Rights Reserved.

   This software is provided AS-IS with no warranty, either express or
   implied.

   This software is distributed under license and may not be copied,
   modified or distributed except as expressly authorized under the terms
   of the license contained in the file LICENSE in this distribution.

   Refer to licensing information at http://www.artifex.com or contact
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
   CA 94129, USA, for further information.
*/

/* RasterOp implementation for 8N-bit memory devices */
#include "memory_.h"
#include "gx.h"
#include "gsbittab.h"
#include "gserrors.h"
#include "gsropt.h"
#include "gxcindex.h"
#include "gxdcolor.h"
#include "gxdevice.h"
#include "gxdevmem.h"
#include "gdevmem.h"
#include "gdevmrop.h"

/* Use the RUN_ROP code. */
#define USE_RUN_ROP
#undef COMPARE_AND_CONTRAST

/*
 * NOTE: The 16- and 32-bit cases aren't implemented: they just fall back to
 * the default implementation.  This is very slow and will be fixed someday.
 */

#define chunk byte

/* Calculate the X offset for a given Y value, */
/* taking shift into account if necessary. */
#define x_offset(px, ty, textures)\
  ((textures)->shift == 0 ? (px) :\
   (px) + (ty) / (textures)->rep_height * (textures)->rep_shift)

/* ---------------- RasterOp with 8-bit gray / 24-bit RGB ---------------- */

int
mem_gray8_rgb24_strip_copy_rop2(gx_device * dev,
             const byte * sdata, int sourcex, uint sraster, gx_bitmap_id id,
                               const gx_color_index * scolors,
           const gx_strip_bitmap * textures, const gx_color_index * tcolors,
                               int x, int y, int width, int height,
                       int phase_x, int phase_y,
                       gs_logical_operation_t dirty_lop, uint planar_height)
{
    gx_device_memory *mdev = (gx_device_memory *) dev;
    gs_logical_operation_t lop = lop_sanitize(dirty_lop);
    gs_rop3_t rop;
    gx_color_index const_source = gx_no_color_index;
    gx_color_index const_texture = gx_no_color_index;
    uint draster = mdev->raster;
    int line_count;
    byte *drow, *base;
    int depth = dev->color_info.depth;
    int bpp = depth >> 3;       /* bytes per pixel, 1 or 3 */
    gx_color_index all_ones = ((gx_color_index) 1 << depth) - 1;
    gx_color_index local_scolors[2];
    gx_color_index local_tcolors[2];

#ifdef USE_RUN_ROP
    rop_run_op ropper;
#ifdef COMPARE_AND_CONTRAST
    static byte testbuffer[4096];
    static byte *start;
    static int bytelen;
#endif
#endif

    /* assert(planar_height == 0); */

    /* Check for constant source. */
    if (!rop3_uses_S(lop)) {
        const_source = 0;       /* arbitrary */
    } else if (scolors != 0 && scolors[0] == scolors[1]) {
        /* Constant source */
        const_source = scolors[0];
        if (const_source == gx_device_black(dev))
            lop = lop_know_S_0(lop);
        else if (const_source == gx_device_white(dev)) {
            lop = lop_know_S_1(lop);
        }
    }

    /* Check for constant texture. */
    if (!rop3_uses_T(lop))
        const_texture = 0;      /* arbitrary */
    else if (tcolors != 0 && tcolors[0] == tcolors[1]) {
        /* Constant texture */
        const_texture = tcolors[0];
        if (const_texture == gx_device_black(dev))
            lop = lop_know_T_0(lop);
        else if (const_texture == gx_device_white(dev)) {
            lop = lop_know_T_1(lop);
        }
    }

    rop = lop_rop(lop);

    if (bpp == 1 &&
        (gx_device_has_color(dev) ||
         (gx_device_black(dev) != 0 || gx_device_white(dev) != all_ones))
        ) {
        /*
         * This is an 8-bit device but not gray-scale.  Except in a few
         * simple cases, we have to use the slow algorithm that converts
         * values to and from RGB.
         */
        gx_color_index bw_pixel;

        switch (rop) {
        case rop3_0:
            bw_pixel = gx_device_black(dev);
            goto bw;
        case rop3_1:
            bw_pixel = gx_device_white(dev);
bw:         if (bw_pixel == 0x00)
                rop = rop3_0;
            else if (bw_pixel == 0xff)
                rop = rop3_1;
            else
                goto df;
            break;
        case rop3_D:
            break;
        case rop3_S:
            break;
        case rop3_T:
            break;
        default:
df:         return mem_default_strip_copy_rop2(dev,
                                               sdata, sourcex, sraster, id,
                                               scolors, textures, tcolors,
                                               x, y, width, height,
                                               phase_x, phase_y, lop, 0);
        }
        /* Put the updated rop back into the lop */
        lop = rop;
    }

    /* Adjust coordinates to be in bounds. */
    if (const_source == gx_no_color_index) {
        fit_copy(dev, sdata, sourcex, sraster, id,
                 x, y, width, height);
    } else {
        fit_fill(dev, x, y, width, height);
    }

    /* Set up transfer parameters. */
    line_count = height;
    base = scan_line_base(mdev, y);
    drow = base + x * bpp;

    /* Allow for colors being passed in with tags in the top bits.
     * This confuses transparency code.
     */
    {
        gx_color_index color_mask = (bpp == 1 ? 0xff : 0xffffff);
        if (scolors)
        {
            local_scolors[0] = scolors[0] & color_mask;
            local_scolors[1] = scolors[1] & color_mask;
            scolors = local_scolors;
        }
        if (tcolors)
        {
            local_tcolors[0] = tcolors[0] & color_mask;
            local_tcolors[1] = tcolors[1] & color_mask;
            tcolors = local_tcolors;
        }
    }

    /*
     * There are 18 cases depending on whether each of the source and
     * texture is constant, 1-bit, or multi-bit, and on whether the
     * depth is 8 or 24 bits.  We divide first according to constant
     * vs. non-constant, and then according to 1- vs. multi-bit, and
     * finally according to pixel depth.  This minimizes source code,
     * but not necessarily time, since we do some of the divisions
     * within 1 or 2 levels of loop.
     */

#ifdef USE_RUN_ROP
#define dbit(base, i) ((base)[(i) >> 3] & (0x80 >> ((i) & 7)))
/* 8-bit */
#define cbit8(base, i, colors)\
  (dbit(base, i) ? (byte)colors[1] : (byte)colors[0])
#ifdef COMPARE_AND_CONTRAST
#define rop_body_8(s_pixel, t_pixel)\
  *dptr = (*rop_proc_table[rop])(*dptr, s_pixel, t_pixel)
#endif
/* 24-bit */
#define get24(ptr)\
  (((gx_color_index)(ptr)[0] << 16) | ((gx_color_index)(ptr)[1] << 8) | (ptr)[2])
#define put24(ptr, pixel)\
  (ptr)[0] = (byte)((pixel) >> 16),\
  (ptr)[1] = (byte)((uint)(pixel) >> 8),\
  (ptr)[2] = (byte)(pixel)
#define cbit24(base, i, colors)\
  (dbit(base, i) ? colors[1] : colors[0])
#ifdef COMPARE_AND_CONTRAST
#define rop_body_24(s_pixel, t_pixel)\
  { gx_color_index d_pixel = get24(dptr);\
    d_pixel = (*rop_proc_table[rop])(d_pixel, s_pixel, t_pixel);\
    put24(dptr, d_pixel);\
  }
#endif
    if (const_texture != gx_no_color_index) {
/**** Constant texture ****/
        if (const_source != gx_no_color_index) {
/**** Constant source & texture ****/
            rop_set_s_constant(&ropper, const_source);
            rop_set_t_constant(&ropper, const_texture);
            if (rop_get_run_op(&ropper, lop, depth, rop_s_constant | rop_t_constant)) {
                for (; line_count-- > 0; drow += draster) {
#ifdef COMPARE_AND_CONTRAST
                    byte *dptr = drow;
                    int left = width;

                    bytelen = left*bpp; start = dptr;
                    memcpy(testbuffer, dptr, bytelen);
                    rop_run(&ropper, testbuffer, left);

                    if (bpp == 1)
/**** 8-bit destination ****/
                        for (; left > 0; ++dptr, --left) {
                            rop_body_8((byte)const_source, (byte)const_texture);
                        }
                    else
/**** 24-bit destination ****/
                        for (; left > 0; dptr += 3, --left) {
                            rop_body_24(const_source, const_texture);
                        }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        emprintf(dev->memory, "Failed!\n");
                    }
#else
                    rop_run(&ropper, drow, width);
#endif
                }
                rop_release_run_op(&ropper);
            }
        } else {
/**** Data source, const texture ****/
            if (scolors) {
                const byte *srow = sdata;

                rop_set_t_constant(&ropper, const_texture);
                rop_set_s_colors(&ropper, scolors);
                if (rop_get_run_op(&ropper, lop, depth, rop_t_constant | rop_s_1bit)) {

                    for (; line_count-- > 0; drow += draster, srow += sraster) {
#ifdef COMPARE_AND_CONTRAST
                        byte *dptr = drow;
                        int left = width;
/**** 1-bit source ****/
                        int sx = sourcex;

                        rop_set_s_bitmap_subbyte(&ropper, srow, sourcex);
                        bytelen = left*bpp; start = dptr;
                        memcpy(testbuffer, dptr, bytelen);
                        rop_run(&ropper, testbuffer, width);
                        if (bpp == 1)
/**** 8-bit destination ****/
                            for (; left > 0; ++dptr, ++sx, --left) {
                                byte s_pixel = cbit8(srow, sx, scolors);

                                rop_body_8(s_pixel, (byte)const_texture);
                            }
                        else
/**** 24-bit destination ****/
                            for (; left > 0; dptr += 3, ++sx, --left) {
                                bits32 s_pixel = cbit24(srow, sx, scolors);

                                rop_body_24(s_pixel, const_texture);
                            }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
/**** 1-bit source ****/
/**** 8-bit destination ****/
/**** 24-bit destination ****/
                        rop_set_s_bitmap_subbyte(&ropper, srow, sourcex);
                        rop_run(&ropper, drow, width);
#endif
                    }
                    rop_release_run_op(&ropper);
                }
            } else {
                const byte *srow = sdata;
                rop_set_t_constant(&ropper, const_texture);
                if (rop_get_run_op(&ropper, lop, depth, rop_t_constant)) {
                    for (; line_count-- > 0; drow += draster, srow += sraster) {
#ifdef COMPARE_AND_CONTRAST
                        byte *dptr = drow;
                        int left = width;

                        bytelen = left*bpp; start = dptr;
                        memcpy(testbuffer, dptr, bytelen);

                        rop_set_s_bitmap(&ropper, srow + sourcex * bpp);
                        rop_run(&ropper, testbuffer, left);
/**** 8-bit source & dest ****/
                        if (bpp == 1) {
                            const byte *sptr = srow + sourcex;

                            for (; left > 0; ++dptr, ++sptr, --left) {
                                byte s_pixel = *sptr;

                                rop_body_8(s_pixel, (byte)const_texture);
                            }
                        } else {
/**** 24-bit source & dest ****/
                            const byte *sptr = srow + sourcex * 3;

                            bytelen = left*bpp; start = dptr;
                            memcpy(testbuffer, dptr, bytelen);

                            for (; left > 0; dptr += 3, sptr += 3, --left) {
                                bits32 s_pixel = get24(sptr);

                                rop_body_24(s_pixel, const_texture);
                            }
                        }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
/**** 8-bit source & dest ****/
/**** 24-bit source & dest ****/
                        rop_set_s_bitmap(&ropper, srow + sourcex * bpp);
                        rop_run(&ropper, drow, width);
#endif
                    }
                    rop_release_run_op(&ropper);
                }
            }
        }
    } else if (const_source != gx_no_color_index) {
/**** Const source, data texture ****/
        if (tcolors) {
            uint traster = textures->raster;
            int ty = y + phase_y;

            rop_set_s_constant(&ropper, const_source);
            rop_set_t_colors(&ropper, tcolors);
            if (rop_get_run_op(&ropper, lop, depth, rop_s_constant | rop_t_1bit)) {
                for (; line_count-- > 0; drow += draster, ++ty) {   /* Loop over copies of the tile. */
                    int dx = x, w = width, nw;
                    byte *dptr = drow;
                    const byte *trow =
                    textures->data + (ty % textures->size.y) * traster;
                    int xoff = x_offset(phase_x, ty, textures);

                    for (; w > 0; dx += nw, w -= nw) {
                        int tx = (dx + xoff) % textures->rep_width;
                        int left = nw = min(w, textures->size.x - tx);
#ifdef COMPARE_AND_CONTRAST
                        const byte *tptr = trow;
#endif

                        rop_set_t_bitmap_subbyte(&ropper, trow, tx);
#ifdef COMPARE_AND_CONTRAST
                        bytelen = left*bpp; start = dptr;
                        memcpy(testbuffer, dptr, bytelen);
                        rop_run(&ropper, testbuffer, left);
/**** 1-bit texture ****/
                        if (bpp == 1)
/**** 8-bit dest ****/
                            for (; left > 0; ++dptr, ++tx, --left) {
                                byte t_pixel = cbit8(tptr, tx, tcolors);

                                rop_body_8((byte)const_source, t_pixel);
                            }
                        else
/**** 24-bit dest ****/
                            for (; left > 0; dptr += 3, ++tx, --left) {
                                bits32 t_pixel = cbit24(tptr, tx, tcolors);

                                rop_body_24(const_source, t_pixel);
                            }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
                        rop_run(&ropper, dptr, left);
                        dptr += left;
#endif
                    }
                }
            }
        } else {
            uint traster = textures->raster;
            int ty = y + phase_y;

            rop_set_s_constant(&ropper, const_source);
            if (rop_get_run_op(&ropper, lop, depth, rop_s_constant)) {

                for (; line_count-- > 0; drow += draster, ++ty) {   /* Loop over copies of the tile. */
                    int dx = x, w = width, nw;
                    byte *dptr = drow;
                    const byte *trow =
                    textures->data + (ty % textures->size.y) * traster;
                    int xoff = x_offset(phase_x, ty, textures);

                    for (; w > 0; dx += nw, w -= nw) {
                        int tx = (dx + xoff) % textures->rep_width;
                        int left = nw = min(w, textures->size.x - tx);
                        const byte *tptr = trow + tx*bpp;
                        rop_set_t_bitmap(&ropper, tptr);
#ifdef COMPARE_AND_CONTRAST
                        bytelen = left*bpp; start = dptr;
                        memcpy(testbuffer, dptr, bytelen);
                        rop_run(&ropper, testbuffer, left);
/**** 8-bit T & D ****/
                        if (bpp == 1) {
                            for (; left > 0; ++dptr, ++tptr, --left) {
                                byte t_pixel = *tptr;

                                rop_body_8((byte)const_source, t_pixel);
                            }
                        } else {
/**** 24-bit T & D ****/
                            for (; left > 0; dptr += 3, tptr += 3, --left) {
                                bits32 t_pixel = get24(tptr);

                                rop_body_24(const_source, t_pixel);
                            }
                        }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
/**** 8-bit T & D ****/
/**** 24-bit T & D ****/
                        rop_run(&ropper, dptr, left);
                        dptr += left * bpp;
#endif
                    }
                }
                rop_release_run_op(&ropper);
            }
        }
    } else {
/**** Data source & texture ****/
        if (scolors != NULL || tcolors != NULL) {
            uint traster = textures->raster;
            int ty = y + phase_y;
            const byte *srow = sdata;

            if (scolors)
                rop_set_s_colors(&ropper, scolors);
            if (tcolors)
                rop_set_s_colors(&ropper, tcolors);
            if (rop_get_run_op(&ropper, lop, depth,
                               ((scolors == NULL ? 0 : rop_s_1bit) |
                                (tcolors == NULL ? 0 : rop_t_1bit)))) {
                /* Loop over scan lines. */
                for (; line_count-- > 0; drow += draster, srow += sraster, ++ty) {  /* Loop over copies of the tile. */
                    int sx = sourcex;
                    int dx = x;
                    int w = width;
                    int nw;
                    byte *dptr = drow;
                    const byte *trow =
                    textures->data + (ty % textures->size.y) * traster;
                    int xoff = x_offset(phase_x, ty, textures);

                    for (; w > 0; dx += nw, w -= nw) {      /* Loop over individual pixels. */
                        int tx = (dx + xoff) % textures->rep_width;
                        int left = nw = min(w, textures->size.x - tx);
                        const byte *sptr = srow + sx*bpp;
                        const byte *tptr = trow + tx*bpp;

                        /*
                         * For maximum speed, we should split this loop
                         * into 7 cases depending on source & texture
                         * depth: (1,1), (1,8), (1,24), (8,1), (8,8),
                         * (24,1), (24,24).  But since we expect these
                         * cases to be relatively uncommon, we just
                         * divide on the destination depth.
                         */
                        if (scolors)
                            rop_set_s_bitmap_subbyte(&ropper, srow, sx);
                        else
                            rop_set_s_bitmap(&ropper, sptr);
                        if (tcolors)
                            rop_set_t_bitmap_subbyte(&ropper, trow, tx);
                        else
                            rop_set_t_bitmap(&ropper, tptr);

#ifdef COMPARE_AND_CONTRAST
                        bytelen = left*bpp; start = dptr;
                        memcpy(testbuffer, dptr, bytelen);
                        rop_run(&ropper, testbuffer, left);
                        if (bpp == 1) {
/**** 8-bit destination ****/
                            for (; left > 0; ++dptr, ++sptr, ++tptr, ++sx, ++tx, --left) {
                                byte s_pixel =
                                    (scolors ? cbit8(srow, sx, scolors) : *sptr);
                                byte t_pixel =
                                    (tcolors ? cbit8(trow, tx, tcolors) : *tptr);

                                rop_body_8(s_pixel, t_pixel);
                            }
                        } else {
/**** 24-bit destination ****/
                            for (; left > 0; dptr += 3, sptr += 3, tptr += 3, ++sx, ++tx, --left) {
                                bits32 s_pixel =
                                    (scolors ? cbit24(srow, sx, scolors) :
                                     get24(sptr));
                                bits32 t_pixel =
                                    (tcolors ? cbit24(tptr, tx, tcolors) :
                                     get24(tptr));

                                rop_body_24(s_pixel, t_pixel);
                            }
                        }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
                        rop_run(&ropper, dptr, left);
                        dptr += left * bpp;
#endif
                    }
                }
            }
        } else {
            uint traster = textures->raster;
            int ty = y + phase_y;
            const byte *srow = sdata;

            /* Loop over scan lines. */
            if (rop_get_run_op(&ropper, lop, depth, 0)) {
                for (; line_count-- > 0; drow += draster, srow += sraster, ++ty) {  /* Loop over copies of the tile. */
                    int sx = sourcex;
                    int dx = x;
                    int w = width;
                    int nw;
                    byte *dptr = drow;
                    const byte *trow =
                    textures->data + (ty % textures->size.y) * traster;
                    int xoff = x_offset(phase_x, ty, textures);

                    for (; w > 0; sx += nw, dx += nw, w -= nw) {      /* Loop over individual pixels. */
                        int tx = (dx + xoff) % textures->rep_width;
                        int left = nw = min(w, textures->size.x - tx);
                        const byte *tptr = trow + tx * bpp;
                        const byte *sptr = srow + sx * bpp;

                        rop_set_s_bitmap(&ropper, sptr);
                        rop_set_t_bitmap(&ropper, tptr);
#ifdef COMPARE_AND_CONTRAST
                        if (bpp == 1) {
                            rop_run(&ropper, testbuffer, left);
/**** 8-bit destination ****/

                            for (; left > 0; ++dptr, ++sptr, ++tptr, ++sx, ++tx, --left) {
                                rop_body_8(*sptr, *tptr);
                            }
                        } else {
/**** 24-bit destination ****/
                            for (; left > 0; dptr += 3, sptr += 3, tptr += 3, ++sx, ++tx, --left) {
                                bits32 s_pixel = get24(sptr);
                                bits32 t_pixel = get24(tptr);

                                rop_body_24(s_pixel, t_pixel);
                            }
                        }
                        if (memcmp(testbuffer, start, bytelen) != 0) {
                            emprintf(dev->memory, "Failed!\n");
                        }
#else
/**** 8-bit destination ****/
/**** 24-bit destination ****/
                        rop_run(&ropper, dptr, left);
                        dptr += left * bpp;
#endif
                    }
                }
                rop_release_run_op(&ropper);
            }
        }
    }
#undef rop_body_8
#undef rop_body_24
#undef dbit
#undef cbit8
#undef cbit24
#else

#define dbit(base, i) ((base)[(i) >> 3] & (0x80 >> ((i) & 7)))
/* 8-bit */
#define cbit8(base, i, colors)\
  (dbit(base, i) ? (byte)colors[1] : (byte)colors[0])
#define rop_body_8(s_pixel, t_pixel)\
  *dptr = (*rop_proc_table[rop])(*dptr, s_pixel, t_pixel)
/* 24-bit */
#define get24(ptr)\
  (((gx_color_index)(ptr)[0] << 16) | ((gx_color_index)(ptr)[1] << 8) | (ptr)[2])
#define put24(ptr, pixel)\
  (ptr)[0] = (byte)((pixel) >> 16),\
  (ptr)[1] = (byte)((uint)(pixel) >> 8),\
  (ptr)[2] = (byte)(pixel)
#define cbit24(base, i, colors)\
  (dbit(base, i) ? colors[1] : colors[0])
#define rop_body_24(s_pixel, t_pixel)\
  { gx_color_index d_pixel = get24(dptr);\
    d_pixel = (*rop_proc_table[rop])(d_pixel, s_pixel, t_pixel);\
    put24(dptr, d_pixel);\
  }

    if (const_texture != gx_no_color_index) {
/**** Constant texture ****/
        if (const_source != gx_no_color_index) {
/**** Constant source & texture ****/
            for (; line_count-- > 0; drow += draster) {
                byte *dptr = drow;
                int left = width;

                if (bpp == 1)
/**** 8-bit destination ****/
                    for (; left > 0; ++dptr, --left) {
                        rop_body_8((byte)const_source, (byte)const_texture);
                    }
                else
/**** 24-bit destination ****/
                    for (; left > 0; dptr += 3, --left) {
                        rop_body_24(const_source, const_texture);
                    }
            }
        } else {
/**** Data source, const texture ****/
            const byte *srow = sdata;

            for (; line_count-- > 0; drow += draster, srow += sraster) {
                byte *dptr = drow;
                int left = width;

                if (scolors) {
/**** 1-bit source ****/
                    int sx = sourcex;

                    if (bpp == 1)
/**** 8-bit destination ****/
                        for (; left > 0; ++dptr, ++sx, --left) {
                            byte s_pixel = cbit8(srow, sx, scolors);

                            rop_body_8(s_pixel, (byte)const_texture);
                        }
                    else
/**** 24-bit destination ****/
                        for (; left > 0; dptr += 3, ++sx, --left) {
                            bits32 s_pixel = cbit24(srow, sx, scolors);

                            rop_body_24(s_pixel, const_texture);
                        }
                } else if (bpp == 1) {
/**** 8-bit source & dest ****/
                    const byte *sptr = srow + sourcex;

                    for (; left > 0; ++dptr, ++sptr, --left) {
                        byte s_pixel = *sptr;

                        rop_body_8(s_pixel, (byte)const_texture);
                    }
                } else {
/**** 24-bit source & dest ****/
                    const byte *sptr = srow + sourcex * 3;

                    for (; left > 0; dptr += 3, sptr += 3, --left) {
                        bits32 s_pixel = get24(sptr);

                        rop_body_24(s_pixel, const_texture);
                    }
                }
            }
        }
    } else if (const_source != gx_no_color_index) {
/**** Const source, data texture ****/
        uint traster = textures->raster;
        int ty = y + phase_y;

        for (; line_count-- > 0; drow += draster, ++ty) {       /* Loop over copies of the tile. */
            int dx = x, w = width, nw;
            byte *dptr = drow;
            const byte *trow =
            textures->data + (ty % textures->size.y) * traster;
            int xoff = x_offset(phase_x, ty, textures);

            for (; w > 0; dx += nw, w -= nw) {
                int tx = (dx + xoff) % textures->rep_width;
                int left = nw = min(w, textures->size.x - tx);
                const byte *tptr = trow;

                if (tcolors) {
/**** 1-bit texture ****/
                    if (bpp == 1)
/**** 8-bit dest ****/
                        for (; left > 0; ++dptr, ++tx, --left) {
                            byte t_pixel = cbit8(tptr, tx, tcolors);

                            rop_body_8((byte)const_source, t_pixel);
                        }
                    else
/**** 24-bit dest ****/
                        for (; left > 0; dptr += 3, ++tx, --left) {
                            bits32 t_pixel = cbit24(tptr, tx, tcolors);

                            rop_body_24(const_source, t_pixel);
                        }
                } else if (bpp == 1) {
/**** 8-bit T & D ****/
                    tptr += tx;
                    for (; left > 0; ++dptr, ++tptr, --left) {
                        byte t_pixel = *tptr;

                        rop_body_8((byte)const_source, t_pixel);
                    }
                } else {
/**** 24-bit T & D ****/
                    tptr += tx * 3;
                    for (; left > 0; dptr += 3, tptr += 3, --left) {
                        bits32 t_pixel = get24(tptr);

                        rop_body_24(const_source, t_pixel);
                    }
                }
            }
        }
    } else {
/**** Data source & texture ****/
        uint traster = textures->raster;
        int ty = y + phase_y;
        const byte *srow = sdata;

        /* Loop over scan lines. */
        for (; line_count-- > 0; drow += draster, srow += sraster, ++ty) {      /* Loop over copies of the tile. */
            int sx = sourcex;
            int dx = x;
            int w = width;
            int nw;
            byte *dptr = drow;
            const byte *trow =
            textures->data + (ty % textures->size.y) * traster;
            int xoff = x_offset(phase_x, ty, textures);

            for (; w > 0; dx += nw, w -= nw) {  /* Loop over individual pixels. */
                int tx = (dx + xoff) % textures->rep_width;
                int left = nw = min(w, textures->size.x - tx);
                const byte *tptr = trow;

                /*
                 * For maximum speed, we should split this loop
                 * into 7 cases depending on source & texture
                 * depth: (1,1), (1,8), (1,24), (8,1), (8,8),
                 * (24,1), (24,24).  But since we expect these
                 * cases to be relatively uncommon, we just
                 * divide on the destination depth.
                 */
                if (bpp == 1) {
/**** 8-bit destination ****/
                    const byte *sptr = srow + sx;

                    tptr += tx;
                    for (; left > 0; ++dptr, ++sptr, ++tptr, ++sx, ++tx, --left) {
                        byte s_pixel =
                            (scolors ? cbit8(srow, sx, scolors) : *sptr);
                        byte t_pixel =
                            (tcolors ? cbit8(tptr, tx, tcolors) : *tptr);

                        rop_body_8(s_pixel, t_pixel);
                    }
                } else {
/**** 24-bit destination ****/
                    const byte *sptr = srow + sx * 3;

                    tptr += tx * 3;
                    for (; left > 0; dptr += 3, sptr += 3, tptr += 3, ++sx, ++tx, --left) {
                        bits32 s_pixel =
                            (scolors ? cbit24(srow, sx, scolors) :
                             get24(sptr));
                        bits32 t_pixel =
                            (tcolors ? cbit24(tptr, tx, tcolors) :
                             get24(tptr));

                        rop_body_24(s_pixel, t_pixel);
                    }
                }
            }
        }
    }
#undef rop_body_8
#undef rop_body_24
#undef dbit
#undef cbit8
#undef cbit24
#endif
    return 0;
}