Exemple #1
0
int
mem_gray8_rgb24_strip_copy_rop(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 lop)
{
    gx_device_memory *mdev = (gx_device_memory *) dev;
    gs_rop3_t rop = lop_rop(lop);
    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 strans =
        (lop & lop_S_transparent ? all_ones : gx_no_color_index);
    gx_color_index ttrans =
        (lop & lop_T_transparent ? all_ones : gx_no_color_index);
#ifdef USE_RUN_ROP
    rop_run_op ropper;
#ifdef COMPARE_AND_CONTRAST
    static byte testbuffer[4096];
    static byte *start;
    static int bytelen;
#endif
#endif

    /* Check for constant source. */
    if (!rop3_uses_S(rop))
        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))
            rop = rop3_know_S_0(rop);
        else if (const_source == gx_device_white(dev))
            rop = rop3_know_S_1(rop);
    }

    /* Check for constant texture. */
    if (!rop3_uses_T(rop))
        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))
            rop = rop3_know_T_0(rop);
        else if (const_texture == gx_device_white(dev))
            rop = rop3_know_T_1(rop);
    }

    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:
            if (lop & lop_S_transparent)
                goto df;
            break;
        case rop3_T:
            if (lop & lop_T_transparent)
                goto df;
            break;
        default:
df:         return mem_default_strip_copy_rop(dev,
                                              sdata, sourcex, sraster, id,
                                              scolors, textures, tcolors,
                                              x, y, width, height,
                                              phase_x, phase_y, lop);
        }
    }

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

    /*
     * 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])
#define rop_body_8(s_pixel, t_pixel)\
  if ( (s_pixel) == strans ||   /* So = 0, s_tr = 1 */\
       (t_pixel) == ttrans      /* Po = 0, p_tr = 1 */\
     )\
    continue;\
  *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)\
  if ( (s_pixel) == strans ||   /* So = 0, s_tr = 1 */\
       (t_pixel) == ttrans      /* Po = 0, p_tr = 1 */\
     )\
    continue;\
  { 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 ****/
            rop_get_run_op(&ropper, lop, depth, rop_s_constant | rop_t_constant);
            rop_set_s_constant(&ropper, const_source);
            rop_set_t_constant(&ropper, const_texture);
            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) {
                        vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        rop_body_8((byte)const_source, (byte)const_texture);
                    }
                else
/**** 24-bit destination ****/
                    for (; left > 0; dptr += 3, --left) {
                        vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        rop_body_24(const_source, const_texture);
                    }
                if (memcmp(testbuffer, start, bytelen) != 0) {
                    eprintf("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_get_run_op(&ropper, lop, depth, rop_t_constant | rop_s_1bit);
                rop_set_t_constant(&ropper, const_texture);
                rop_set_s_colors(&ropper, scolors);

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

                            vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                            rop_body_24(s_pixel, const_texture);
                        }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        eprintf("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_get_run_op(&ropper, lop, depth, rop_t_constant);
                rop_set_t_constant(&ropper, const_texture);
                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;

                            vd_pixel(int2fixed((dptr - base) % draster),
                                     int2fixed((dptr - base) / draster + y), const_texture);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                     int2fixed((dptr - base) / draster + y), const_texture);
                            rop_body_24(s_pixel, const_texture);
                        }
                    }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        eprintf("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_get_run_op(&ropper, lop, depth, rop_s_constant | rop_t_1bit);
            rop_set_s_constant(&ropper, const_source);
            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;

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

                            vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                            rop_body_24(const_source, t_pixel);
                        }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        eprintf("Failed!\n");
                    }
#else
                    rop_run(&ropper, dptr, left);
                    dptr += left;
#endif
                }
            }
        } else {
            uint traster = textures->raster;
            int ty = y + phase_y;

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

            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;

                            vd_pixel(int2fixed((dptr - base) % draster),
                                    int2fixed((dptr - base) / draster + y), t_pixel);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                     int2fixed((dptr - base) / draster + y), t_pixel);
                            rop_body_24(const_source, t_pixel);
                        }
                    }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        eprintf("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;

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

                            vd_pixel(int2fixed((dptr - base) % draster),
                                     int2fixed((dptr - base) / draster + y), t_pixel);
                            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));

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                     int2fixed((dptr - base) / draster + y), t_pixel);
                            rop_body_24(s_pixel, t_pixel);
                        }
                    }
                    if (memcmp(testbuffer, start, bytelen) != 0) {
                        eprintf("Failed!\n");
                    }
#else
                    rop_run(&ropper, dptr, left);
#endif
                }
            }
        } else {
            uint traster = textures->raster;
            int ty = y + phase_y;
            const byte *srow = sdata;

            /* Loop over scan lines. */
            rop_get_run_op(&ropper, rop, 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; 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) {
                        eprintf("Failed!\n");
                    }
#else
/**** 8-bit destination ****/
/**** 24-bit destination ****/
                    rop_run(&ropper, dptr, left);
#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)\
  if ( (s_pixel) == strans ||   /* So = 0, s_tr = 1 */\
       (t_pixel) == ttrans      /* Po = 0, p_tr = 1 */\
     )\
    continue;\
  *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)\
  if ( (s_pixel) == strans ||   /* So = 0, s_tr = 1 */\
       (t_pixel) == ttrans      /* Po = 0, p_tr = 1 */\
     )\
    continue;\
  { 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) {
                        vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        rop_body_8((byte)const_source, (byte)const_texture);
                    }
                else
/**** 24-bit destination ****/
                    for (; left > 0; dptr += 3, --left) {
                        vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        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);

                            vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                            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;

                        vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        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);

                        vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), const_texture);
                        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);

                            vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                            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);

                            vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                            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;

                        vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                        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);

                        vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                        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);

                        vd_pixel(int2fixed((dptr - base) % draster),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                        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));

                        vd_pixel(int2fixed((dptr - base) % draster / 3),
                                 int2fixed((dptr - base) / draster + y), t_pixel);
                        rop_body_24(s_pixel, t_pixel);
                    }
                }
            }
        }
    }
#undef rop_body_8
#undef rop_body_24
#undef dbit
#undef cbit8
#undef cbit24
#endif
    return 0;
}
/* Stroke the current path */
int
gs_stroke(gs_state * pgs)
{
    int code;

    /*
     * If we're inside a charpath, just merge the current path
     * into the parent's path.
     */
    if (pgs->in_charpath) {
	if (pgs->in_charpath == cpm_true_charpath) {
	    /*
	     * A stroke inside a true charpath should do the
	     * equivalent of strokepath.
	     */
	    code = gs_strokepath(pgs);
	    if (code < 0)
		return code;
	}
	code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
				     pgs->in_charpath);
    }
    if (gs_is_null_device(pgs->device)) {
	/* Handle separately to prevent gs_state_color_load. */
	gs_newpath(pgs);
	code = 0;
    } else {
	int abits, acode, rcode = 0;

        /* to distinguish text from vectors we hackly look at the
           target device 1 bit per component is a cache and this is
           text else it is a path */
        if (gx_device_has_color(gs_currentdevice(pgs)))
            gs_set_object_tag(pgs, GS_PATH_TAG);
        else
            gs_set_object_tag(pgs, GS_TEXT_TAG);

	/* Here we need to distinguish text from vectors to compute the object tag.
	   Actually we need to know whether this function is called to rasterize a character,
	   or to rasterize a vector graphics to the output device.
	   Currently we assume it works for the bitrgbtags device only,
	   which is a low level device with a 4-component color model.
	   We use the fact that with printers a character is usually being rendered 
	   to a 1bpp cache device rather than to the output device.
	   Therefore we hackly look whether the target device
	   "has a color" : either it's a multicomponent color model,
	   or it is not gray (such as a yellow separation).

	   This check has several limitations :
	   1. It doesn't work with -dNOCACHE.
	   2. It doesn't work with large characters,
	      which cannot fit into a cache cell and thus they
	      render directly to the output device.
	   3. It doesn't work for TextAlphaBits=2 or 4.
	      We don't care of this case because
	      text antialiasing usually usn't applied to printers.
	   4. It doesn't work for things like with "(xyz) true charpath stroke".
	      That's unfortunate, we'd like to improve someday.
	   5. It doesn't work for high level devices when a Type 3 character is being constructed.
	      This case is not important for low level devices
	      (which a printer is), because low level device doesn't accept
	      Type 3 charproc streams immediately.
	 */
        if (gx_device_has_color(gs_currentdevice(pgs))) {
            gs_set_object_tag(pgs, GS_PATH_TAG);
	}
	else {
            gs_set_object_tag(pgs, GS_TEXT_TAG);
	}
	gx_set_dev_color(pgs);
	code = gs_state_color_load(pgs);
	if (code < 0)
	    return code;
	abits = alpha_buffer_bits(pgs);
	if (abits > 1) {
	    /*
	     * Expand the bounding box by the line width.
	     * This is expensive to compute, so we only do it
	     * if we know we're going to buffer.
	     */
	    float xxyy = fabs(pgs->ctm.xx) + fabs(pgs->ctm.yy);
	    float xyyx = fabs(pgs->ctm.xy) + fabs(pgs->ctm.yx);
	    float scale = (float)(1 << (abits / 2));
	    float orig_width = gs_currentlinewidth(pgs);
	    float new_width = orig_width * scale;
	    fixed extra_adjust =
		float2fixed(max(xxyy, xyyx) * new_width / 2);
	    float orig_flatness = gs_currentflat(pgs);
	    gx_path spath;

	    /* Scale up the line width, dash pattern, and flatness. */
	    if (extra_adjust < fixed_1)
		extra_adjust = fixed_1;
	    acode = alpha_buffer_init(pgs,
				      pgs->fill_adjust.x + extra_adjust,
				      pgs->fill_adjust.y + extra_adjust,
				      abits);
	    if (acode < 0)
		return acode;
	    gs_setlinewidth(pgs, new_width);
	    scale_dash_pattern(pgs, scale);
	    gs_setflat(pgs, orig_flatness * scale);
	    /*
	     * The alpha-buffer device requires that we fill the
	     * entire path as a single unit.
	     */
	    gx_path_init_local(&spath, pgs->memory);
	    code = gx_stroke_add(pgs->path, &spath, pgs);
	    gs_setlinewidth(pgs, orig_width);
	    scale_dash_pattern(pgs, 1.0 / scale);
	    if (code >= 0)
		code = gx_fill_path(&spath, pgs->dev_color, pgs,
				    gx_rule_winding_number,
				    pgs->fill_adjust.x,
				    pgs->fill_adjust.y);
	    gs_setflat(pgs, orig_flatness);
	    gx_path_free(&spath, "gs_stroke");
	    if (acode > 0)
		rcode = alpha_buffer_release(pgs, code >= 0);
	} else
	    code = gx_stroke_fill(pgs->path, pgs);
	if (code >= 0)
	    gs_newpath(pgs);
	if (code >= 0 && rcode < 0)
	    code = rcode;
    }
    return code;
}
Exemple #3
0
/* This routine is used for all formats. */
static int
png_print_page(gx_device_printer * pdev, FILE * file)
{
    gs_memory_t *mem = pdev->memory;
    int raster = gdev_prn_raster(pdev);

    /* PNG structures */
    byte *row = gs_alloc_bytes(mem, raster, "png raster buffer");
    png_struct *png_ptr =
    png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    png_info *info_ptr =
    png_create_info_struct(png_ptr);
    int height = pdev->height;
    int depth = pdev->color_info.depth;
    int y;
    int code;			/* return code */
    char software_key[80];
    char software_text[256];
    png_text text_png;

    if (row == 0 || png_ptr == 0 || info_ptr == 0) {
	code = gs_note_error(gs_error_VMerror);
	goto done;
    }
    /* set error handling */
    if (setjmp(png_ptr->jmpbuf)) {
	/* If we get here, we had a problem reading the file */
	code = gs_note_error(gs_error_VMerror);
	goto done;
    }
    code = 0;			/* for normal path */
    /* set up the output control */
    png_init_io(png_ptr, file);

    /* set the file information here */
    info_ptr->width = pdev->width;
    info_ptr->height = pdev->height;
    /* resolution is in pixels per meter vs. dpi */
    info_ptr->x_pixels_per_unit =
	(png_uint_32) (pdev->HWResolution[0] * (100.0 / 2.54));
    info_ptr->y_pixels_per_unit =
	(png_uint_32) (pdev->HWResolution[1] * (100.0 / 2.54));
    info_ptr->phys_unit_type = PNG_RESOLUTION_METER;
    info_ptr->valid |= PNG_INFO_pHYs;
    switch (depth) {
	case 32:
	    info_ptr->bit_depth = 8;
	    info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
	    png_set_invert_alpha(png_ptr);
	    {   gx_device_pngalpha *ppdev = (gx_device_pngalpha *)pdev;
		png_color_16 background;
		background.index = 0;
		background.red =   (ppdev->background >> 16) & 0xff;
		background.green = (ppdev->background >> 8)  & 0xff;
		background.blue =  (ppdev->background)       & 0xff;
		background.gray = 0;
		png_set_bKGD(png_ptr, info_ptr, &background);
	    }
	    break;
	case 48:
	    info_ptr->bit_depth = 16;
	    info_ptr->color_type = PNG_COLOR_TYPE_RGB;
#if defined(ARCH_IS_BIG_ENDIAN) && (!ARCH_IS_BIG_ENDIAN) 
	    png_set_swap(png_ptr);
#endif
	    break;
	case 24:
	    info_ptr->bit_depth = 8;
	    info_ptr->color_type = PNG_COLOR_TYPE_RGB;
	    break;
	case 8:
	    info_ptr->bit_depth = 8;
	    if (gx_device_has_color(pdev))
		info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
	    else
		info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
	    break;
	case 4:
	    info_ptr->bit_depth = 4;
	    info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
	    break;
	case 1:
	    info_ptr->bit_depth = 1;
	    info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
	    /* invert monocrome pixels */
	    png_set_invert_mono(png_ptr);
	    break;
    }

    /* set the palette if there is one */
    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) {
	int i;
	int num_colors = 1 << depth;
	gx_color_value rgb[3];

	info_ptr->palette =
	    (void *)gs_alloc_bytes(mem, 256 * sizeof(png_color),
				   "png palette");
	if (info_ptr->palette == 0) {
	    code = gs_note_error(gs_error_VMerror);
	    goto done;
	}
	info_ptr->num_palette = num_colors;
	info_ptr->valid |= PNG_INFO_PLTE;
	for (i = 0; i < num_colors; i++) {
	    (*dev_proc(pdev, map_color_rgb)) ((gx_device *) pdev,
					      (gx_color_index) i, rgb);
	    info_ptr->palette[i].red = gx_color_value_to_byte(rgb[0]);
	    info_ptr->palette[i].green = gx_color_value_to_byte(rgb[1]);
	    info_ptr->palette[i].blue = gx_color_value_to_byte(rgb[2]);
	}
    }
    /* add comment */
    strncpy(software_key, "Software", sizeof(software_key));
    sprintf(software_text, "%s %d.%02d", gs_product,
	    (int)(gs_revision / 100), (int)(gs_revision % 100));
    text_png.compression = -1;	/* uncompressed */
    text_png.key = software_key;
    text_png.text = software_text;
    text_png.text_length = strlen(software_text);
    info_ptr->text = &text_png;
    info_ptr->num_text = 1;

    /* write the file information */
    png_write_info(png_ptr, info_ptr);

    /* don't write the comments twice */
    info_ptr->num_text = 0;
    info_ptr->text = NULL;

    /* Write the contents of the image. */
    for (y = 0; y < height; y++) {
	gdev_prn_copy_scan_lines(pdev, y, row, raster);
	png_write_rows(png_ptr, &row, 1);
    }

    /* write the rest of the file */
    png_write_end(png_ptr, info_ptr);

    /* if you alloced the palette, free it here */
    gs_free_object(mem, info_ptr->palette, "png palette");

  done:
    /* free the structures */
    png_destroy_write_struct(&png_ptr, &info_ptr);
    gs_free_object(mem, row, "png raster buffer");

    return code;
}
/* Fill the current path using a specified rule. */
static int
fill_with_rule(gs_state * pgs, int rule)
{
    int code;

    /* If we're inside a charpath, just merge the current path */
    /* into the parent's path. */
    if (pgs->in_charpath)
	code = gx_path_add_char_path(pgs->show_gstate->path, pgs->path,
				     pgs->in_charpath);
    else if (gs_is_null_device(pgs->device)) {
	/* Handle separately to prevent gs_state_color_load - bug 688308. */
	gs_newpath(pgs);
	code = 0;
    } else {
	int abits, acode, rcode = 0;

	/* Here we need to distinguish text from vectors to compute the object tag.
	   Actually we need to know whether this function is called to rasterize a character,
	   or to rasterize a vector graphics to the output device.
	   Currently we assume it works for the bitrgbtags device only,
	   which is a low level device with a 4-component color model.
	   We use the fact that with printers a character is usually being rendered 
	   to a 1bpp cache device rather than to the output device.
	   Therefore we hackly look whether the target device
	   "has a color" : either it's a multicomponent color model,
	   or it is not gray (such as a yellow separation).

	   This check has several limitations :
	   1. It doesn't work with -dNOCACHE.
	   2. It doesn't work with large characters,
	      which cannot fit into a cache cell and thus they
	      render directly to the output device.
	   3. It doesn't work for TextAlphaBits=2 or 4.
	      We don't care of this case because
	      text antialiasing usually usn't applied to printers.
	   4. It doesn't work for things like with "(xyz) true charpath stroke".
	      That's unfortunate, we'd like to improve someday.
	   5. It doesn't work for high level devices when a Type 3 character is being constructed.
	      This case is not important for low level devices
	      (which a printer is), because low level device doesn't accept
	      Type 3 charproc streams immediately.
	   6. It doesn't work properly while an insiding testing,
	      which sets gs_hit_device, which is uncolored.
	 */
        if (gx_device_has_color(gs_currentdevice(pgs))) {
            gs_set_object_tag(pgs, GS_PATH_TAG);
	}
	else {
            gs_set_object_tag(pgs, GS_TEXT_TAG);
	}
	gx_set_dev_color(pgs);
	code = gs_state_color_load(pgs);
	if (code < 0)
	    return code;
	abits = alpha_buffer_bits(pgs);
	if (abits > 1) {
	    acode = alpha_buffer_init(pgs, pgs->fill_adjust.x,
				      pgs->fill_adjust.y, abits);
	    if (acode < 0)
		return acode;
	} else
	    acode = 0;
	code = gx_fill_path(pgs->path, pgs->dev_color, pgs, rule,
			    pgs->fill_adjust.x, pgs->fill_adjust.y);
	if (acode > 0)
	    rcode = alpha_buffer_release(pgs, code >= 0);
	if (code >= 0)
	    gs_newpath(pgs);
	if (code >= 0 && rcode < 0)
	    code = rcode;

    }
    return code;
}