static inline int mark_margin_interior(line_list * ll, margin_set * set, active_line * flp, active_line * alp, fixed y, fixed y0, fixed y1) { section *sect = set->sect; fixed x0 = (y == y0 ? flp->x_current : y == y1 ? flp->x_next : AL_X_AT_Y(flp, y)); fixed x1 = (y == y0 ? alp->x_current : y == y1 ? alp->x_next : AL_X_AT_Y(alp, y)); int i0 = fixed2int(x0), ii0, ii1, i, code; if (int2fixed(i0) + fixed_half < x0) i0++; ii0 = i0 - ll->bbox_left; ii1 = fixed2int_var_pixround(x1) - ll->bbox_left; if (ii0 < ii1) { if (ii0 < 0 || ii1 > ll->bbox_width) return_error(gs_error_unregistered); /* Must not happen. */ for (i = ii0; i < ii1; i++) { sect[i].y0 = sect[i].y1 = -2; vd_circle(int2fixed(i + ll->bbox_left) + fixed_half, y, 3, RGB(255, 0, 0)); } code = store_margin(ll, set, ii0, ii1); if (code < 0) return code; } return 0; }
static int fill_margin(gx_device * dev, const line_list * ll, margin_set *ms, int i0, int i1) { /* Returns the new index (positive) or return code (negative). */ section *sect = ms->sect; int iy = fixed2int_var_pixround(ms->y); int i, ir, h = -2, code; const fill_options * const fo = ll->fo; const bool FILL_DIRECT = fo->fill_direct; if (i0 < 0 || i1 > ll->bbox_width) return_error(gs_error_unregistered); /* Must not happen. */ ir = i0; for (i = i0; i < i1; i++) { int y0 = sect[i].y0, y1 = sect[i].y1, hh; if (y0 == -1) y0 = 0; if (y1 == -1) y1 = fixed_scale - 1; hh = compute_padding(§[i]); # if ADJUST_SERIF if (hh >= 0) { # if !CHECK_SPOT_CONTIGUITY if (i == i0 && i + 1 < i1) { int hhh = compute_padding(§[i + 1]); hh = hhh; } else if (i == i1 - 1 && i > i0) hh = h; /* We could optimize it with moving outside the cycle. * Delaying the optimization until the code is well tested. */ # else if (sect[i].x0 > 0 && sect[i].x1 == fixed_1 && i + 1 < i1) { # if INTERTRAP_STEM_BUG int hhh = hh; # endif hh = (i + 1 < i1 ? compute_padding(§[i + 1]) : -2); /* We could cache hh. * Delaying the optimization until the code is well tested. */ # if INTERTRAP_STEM_BUG /* A bug in the old code. */ if (i > i0 && i + 1 < i1 && hh == -2 && compute_padding(§[i - 1]) == -2) { /* It can be either a thin stem going from left to up or down (See 'r' in 01-001.ps in 'General', ppmraw, 72dpi), or a serif from the left. Since it is between 2 trapezoids, it is better to paint it against a dropout. */ hh = hhh; } # endif } else if (sect[i].x0 == 0 && sect[i].x1 < fixed_1) { # if INTERTRAP_STEM_BUG int hhh = hh; # endif hh = h; # if INTERTRAP_STEM_BUG /* A bug in the old code. */ if (i > i0 && i + 1 < i1 && hh == -2 && compute_padding(§[i - 1]) == -2) { /* It can be either a thin stem going from right to up or down (See 'r' in 01-001.ps in 'General', ppmraw, 72dpi), or a serif from the right. Since it is between 2 trapezoids, it is better to paint it. against a dropout. */ DO_NOTHING; } # endif } # endif } # endif if (h != hh) { if (h >= 0) { VD_RECT(ir + ll->bbox_left, iy + h, i - ir, 1, VD_MARG_COLOR); code = LOOP_FILL_RECTANGLE_DIRECT(fo, ir + ll->bbox_left, iy + h, i - ir, 1); if (code < 0) return code; } ir = i; h = hh; } } if (h >= 0) { VD_RECT(ir + ll->bbox_left, iy + h, i - ir, 1, VD_MARG_COLOR); code = LOOP_FILL_RECTANGLE_DIRECT(fo, ir + ll->bbox_left, iy + h, i - ir, 1); if (code < 0) return code; } init_section(sect, i0, i1); return 0; /* * We added the ADJUST_SERIF feature for small fonts, which are poorly hinted. * An example is 033-52-5873.pdf at 72 dpi. * We either suppress a serif or move it up or down for 1 pixel. * If we would paint it as an entire pixel where it occures, it looks too big * relatively to the character size. Besides, a stem end may * be placed a little bit below the baseline, and our dropout prevention * method desides to paint a pixel below baseline, so that it looks * fallen down (or fallen up in the case of character top). * * We assume that contacting margins are merged in margin_list. * This implies that areas outside a margin are not painted * (Only useful without CHECK_SPOT_CONTIGUITY). * * With no CHECK_SPOT_CONTIGUITY we can't perfectly handle the case when 2 serifs * contact each another inside a margin interior (such as Serif 'n'). * Since we don't know the contiguty, we misrecognize them as a stem and * leave them as they are (possibly still fallen down or up). * * CHECK_SPOT_CONTIGUITY computes the contiguity of the intersection of the spot * and the section window. It allows to recognize contacting serifs properly. * * If a serif isn't painted with regular trapezoids, * it appears a small one, so we don't need to measure its size. * This heuristic isn't perfect, but it is very fast. * Meanwhile with CHECK_SPOT_CONTIGUITY we actually have something * like a bbox for a small serif, and a rough estimation is possible. * * We believe that in normal cases this stuff should work idle, * because a perfect rendering should either use anti-aliasing * (so that the character isn't small in the subpixel grid), * and/or the path must be well fitted into the grid. So please consider * this code as an attempt to do our best for the case of a * non-well-setup rendering. */ }
int gxht_thresh_image_init(gx_image_enum *penum) { int code = 0; fixed ox, oy; int temp; int dev_width, max_height; int spp_out; int k; gx_ht_order *d_order; if (gx_device_must_halftone(penum->dev)) { if (penum->pis != NULL && penum->pis->dev_ht != NULL) { for (k = 0; k < penum->pis->dev_ht->num_comp; k++) { d_order = &(penum->pis->dev_ht->components[k].corder); code = gx_ht_construct_threshold(d_order, penum->dev, penum->pis, k); if (code < 0 ) { return gs_rethrow(code, "threshold creation failed"); } } } else { return -1; } } spp_out = penum->dev->color_info.num_components; /* If the image is landscaped then we want to maintain a buffer that is sufficiently large so that we can hold a byte of halftoned data along the column. This way we avoid doing multiple writes into the same position over and over. The size of the buffer we need depends upon the bitdepth of the output device, the number of device coloranants and the number of colorants in the source space. Note we will need to eventually consider multi-level halftone case here too. For now, to make use of the SSE2 stuff, we would like to have 16 bytes of data to process at a time. So we will collect the columns of data in a buffer that is 16 wide. We will also keep track of the widths of each column. When the total width count reaches 16, we will create our threshold array and apply it. We may have one column that is buffered between calls in this case. Also if a call is made with h=0 we will flush the buffer as we are at the end of the data. */ if (penum->posture == image_landscape) { int col_length = fixed2int_var_rounded(any_abs(penum->x_extent.y)) * spp_out; ox = dda_current(penum->dda.pixel0.x); oy = dda_current(penum->dda.pixel0.y); temp = (int) ceil((float) col_length/16.0); penum->line_size = temp * 16; /* The stride */ /* Now we need at most 16 of these */ penum->line = gs_alloc_bytes(penum->memory, 16 * penum->line_size + 16, "gxht_thresh"); /* Same with this */ penum->thresh_buffer = gs_alloc_bytes(penum->memory, penum->line_size * 16 + 16, "gxht_thresh"); /* That maps into 2 bytes of Halftone data */ penum->ht_buffer = gs_alloc_bytes(penum->memory, penum->line_size * 2, "gxht_thresh"); penum->ht_stride = penum->line_size; if (penum->line == NULL || penum->thresh_buffer == NULL || penum->ht_buffer == NULL) return -1; penum->ht_landscape.count = 0; penum->ht_landscape.num_contones = 0; if (penum->y_extent.x < 0) { /* Going right to left */ penum->ht_landscape.curr_pos = 15; penum->ht_landscape.index = -1; } else { /* Going left to right */ penum->ht_landscape.curr_pos = 0; penum->ht_landscape.index = 1; } if (penum->x_extent.y < 0) { penum->ht_landscape.flipy = true; penum->ht_landscape.y_pos = fixed2int_pixround_perfect(dda_current(penum->dda.pixel0.y) + penum->x_extent.y); } else { penum->ht_landscape.flipy = false; penum->ht_landscape.y_pos = fixed2int_pixround_perfect(dda_current(penum->dda.pixel0.y)); } memset(&(penum->ht_landscape.widths[0]), 0, sizeof(int)*16); penum->ht_landscape.offset_set = false; penum->ht_offset_bits = 0; /* Will get set in call to render */ if (code >= 0) { #if defined(DEBUG) || defined(PACIFY_VALGRIND) memset(penum->line, 0, 16 * penum->line_size + 16); memset(penum->ht_buffer, 0, penum->line_size * 2); memset(penum->thresh_buffer, 0, 16 * penum->line_size + 16); #endif } } else { /* In the portrait case we allocate a single line buffer in device width, a threshold buffer of the same size and possibly wider and the buffer for the halftoned bits. We have to do a bit of work to enable 16 byte boundary after an offset to ensure that we can make use of the SSE2 operations for thresholding. We do the allocations now to avoid doing them with every line */ /* Initialize the ht_landscape stuff to zero */ memset(&(penum->ht_landscape), 0, sizeof(ht_landscape_info_t)); ox = dda_current(penum->dda.pixel0.x); oy = dda_current(penum->dda.pixel0.y); dev_width = (int) fabs((long) fixed2long_pixround(ox + penum->x_extent.x) - fixed2long_pixround(ox)); /* Get the bit position so that we can do a copy_mono for the left remainder and then 16 bit aligned copies for the rest. The right remainder will be OK as it will land in the MSBit positions. Note the #define chunk bits16 in gdevm1.c. Allow also for a 15 sample over run. */ penum->ht_offset_bits = (-fixed2int_var_pixround(ox)) & 15; if (penum->ht_offset_bits > 0) { penum->ht_stride = ((7 + (dev_width + 4)) / 8) + ARCH_SIZEOF_LONG; } else { penum->ht_stride = ((7 + (dev_width + 2)) / 8) + ARCH_SIZEOF_LONG; } /* We want to figure out the maximum height that we may have in taking a single source row and going to device space */ max_height = (int) ceil(fixed2float(any_abs(penum->dst_height)) / (float) penum->Height); penum->ht_buffer = gs_alloc_bytes(penum->memory, penum->ht_stride * max_height * spp_out, "gxht_thresh"); /* We want to have 128 bit alignement for our contone and threshold strips so that we can use SSE operations in the threshold operation. Add in a minor buffer and offset to ensure this. If gs_alloc_bytes provides at least 16 bit alignment so we may need to move 14 bytes. However, the HT process is split in two operations. One that involves the HT of a left remainder and the rest which ensures that we pack in the HT data in the bits with no skew for a fast copy into the gdevm1 device (16 bit copies). So, we need to account for those pixels which occur first and which are NOT aligned for the contone buffer. After we offset by this remainder portion we should be 128 bit aligned. Also allow a 15 sample over run during the execution. */ temp = (int) ceil((float) ((dev_width + 15.0) + 15.0)/16.0); penum->line_size = temp * 16; /* The stride */ penum->line = gs_alloc_bytes(penum->memory, penum->line_size * spp_out, "gxht_thresh"); penum->thresh_buffer = gs_alloc_bytes(penum->memory, penum->line_size * max_height * spp_out, "gxht_thresh"); if (penum->line == NULL || penum->thresh_buffer == NULL || penum->ht_buffer == NULL) { return -1; } else { #if defined(DEBUG) || defined(PACIFY_VALGRIND) memset(penum->line, 0, penum->line_size * spp_out); memset(penum->ht_buffer, 0, penum->ht_stride * max_height * spp_out); memset(penum->thresh_buffer, 0, penum->line_size * max_height * spp_out); #endif } } /* Precompute values needed for rasterizing. */ penum->dxx = float2fixed(penum->matrix.xx + fixed2float(fixed_epsilon) / 2); return code; }