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
|
/* Copyright (C) 1995 Aladdin Enterprises. All rights reserved.
This file is part of GNU Ghostscript.
GNU Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer to
the GNU General Public License for full details.
Everyone is granted permission to copy, modify and redistribute GNU
Ghostscript, but only under the conditions described in the GNU General
Public License. A copy of this license is supposed to have been given to
you along with GNU Ghostscript so you can know your rights and
responsibilities. It should be in a file named COPYING. Among other
things, the copyright notice and this notice must be preserved on all
copies.
Aladdin Enterprises is not affiliated with the Free Software Foundation or
the GNU Project. GNU Ghostscript, as distributed by Aladdin Enterprises,
does not depend on any other GNU software.
*/
/* gdevrun.c */
/* Run-length encoded "device" */
#include "memory_.h"
#include "gx.h"
#include "gserrors.h"
#include "gxdevice.h"
#include "gxdevmem.h"
/*
* The pseudo-device in this file stores 8-bit "pixels" with run-length
* encoding. Since it may allocate less space than is required to
* store all possible values, it may have to discard some update requests.
*/
/*
* Define the representation of each run. We store runs in a doubly-
* linked list using the old trick of storing only a single pointer which
* is the xor of the successor and predecessor indices.
* Run 0 is a dummy end-of-line run; run 1 is a dummy start-of-line run.
* The dummy runs have length 255 to prevent merging.
*/
typedef byte run_length;
typedef byte run_value;
typedef ushort run_index;
typedef struct run_s {
run_length length;
run_value value;
run_index nix; /* for allocated runs, xor of successor and */
/* predecessor indices; for free runs, */
/* index of next free run */
} run;
/*
* Define a pointer into a run list. The xor trick requires that we
* store both the current index and the next (or previous) one.
* For speed, we keep both the index of and the pointer to the current run.
*/
typedef struct run_ptr_s {
run *ptr;
run_index index; /* index of current run */
run_index next; /* index of next run */
} run_ptr;
typedef struct const_run_ptr_s {
const run *ptr;
run_index index; /* index of current run */
run_index next; /* index of next run */
} const_run_ptr;
/* Accessors */
#define rp_length(rp) ((rp).ptr->length)
#define rp_value(rp) ((rp).ptr->value)
#define rp_nix(rp) ((rp).ptr->nix)
/* Traversers */
#define rp_at_start(rp) ((rp).index == 1)
#define rp_at_end(rp) ((rp).index == 0)
#define rp_start(rp, data)\
((rp).index = (data)[1].nix,\
(rp).ptr = (data) + (rp).index,\
(rp).next = rp_nix(rp) ^ 1)
/* Note that rp_next and rp_prev allow rpn == rpc. */
#define rp_next(rpc, data, rpn, itemp)\
(itemp = (rpc).index,\
(rpn).ptr = (data) + ((rpn).index = (rpc).next),\
(rpn).next = itemp ^ rp_nix(rpn))
#define rp_prev(rpc, data, rpp, itemp)\
(itemp = (rpc).next ^ rp_nix(rpc),\
(rpp).next = (rpc).index,\
(rpp).ptr = (data) + ((rpp).index = itemp))
/* Insert/delete */
#define rp_delete_next(rpc, data, line, rpn, rpn2, itemp)\
(rp_next(rpc, data, rpn, itemp),\
rp_next(rpn, data, rpn2, itemp),\
rp_nix(rpc) ^= (rpn).index ^ (rpn2).index,\
rp_nix(rpn2) ^= (rpn).index ^ (rpc).index,\
rp_nix(rpn) = (line)->free,\
(line)->free = (rpn).index)
#define rp_insert_next(rpc, data, line, rpn, itemp)\
(rp_next(rpc, data, rpn, itemp),\
itemp = (line)->free,\
rp_nix(rpc) ^= (rpn).index ^ itemp,\
rp_nix(rpn) ^= (rpc).index ^ itemp,\
(rpn).next = (rpn).index,\
(rpn).index = itemp,\
(rpn).ptr = (data) + itemp,\
(line)->free = rp_nix(rpn),\
rp_nix(rpn) = (rpc).index ^ (rpn).next)
#define rp_insert_prev(rpc, data, line, rpp, itemp)\
(rp_prev(rpc, data, rpp, itemp),\
itemp = (line)->free,\
rp_nix(rpc) ^= (rpp).index ^ itemp,\
rp_nix(rpp) ^= (rpc).index ^ itemp,\
(rpp).ptr = (data) + itemp,\
rp_nix(rpp) = (rpp).index ^ (rpc).index,\
(rpp).index = itemp,\
(line)->free = rp_nix(rpp))
/*
* Define the state of a single scan line.
*
* We maintain the following invariant: if two adjacent runs have the
* same value, the sum of their lengths is at least 256. This may miss
* optimality by nearly a factor of 2, but it's far easier to maintain
* than a true optimal representation.
*
* For speed in the common case where nothing other than 0 is ever stored,
* we initially don't bother to construct the runs (or the free run list)
* for a line at all.
*/
typedef struct run_line_s {
run *data; /* base of runs */
int zero; /* 0 if line not initialized, -1 if initialized */
uint xcur; /* x value at cursor position */
run_ptr rpcur; /* cursor */
run_index free; /* head of free list */
} run_line;
/*
* Define the device, built on an 8-bit memory device.
*/
typedef struct gx_device_run_s {
gx_device_memory md;
uint runs_per_line;
run_line *lines;
int umin, umax1; /* some range of uninitialized lines */
} gx_device_run;
#define rdev ((gx_device_run *)dev)
/* Open the device. */
private int
run_open(gx_device *dev)
{ run_line *line = rdev->lines;
run *data = (run *)rdev->md.base;
int i;
/*
* We need ceil(width / 255) runs to represent a line where all
* elements have the same value, +2 for the start and end runs,
* +2 for the check for 2 free runs when doing a replacement.
*/
if ( rdev->runs_per_line < (dev->width + 254) / 255 + 4 )
return_error(gs_error_rangecheck);
for ( i = 0; i < dev->height; ++line, data += rdev->runs_per_line, ++i )
{ line->data = data;
line->zero = 0;
}
rdev->umin = 0;
rdev->umax1 = dev->height;
return 0;
}
/* Finish initializing a line. This is a separate procedure only */
/* for readability. */
private void
run_line_initialize(gx_device *dev, int y)
{ run_line *line = &rdev->lines[y];
run *data = line->data;
int left = dev->width;
run_index index = 2;
run *rcur;
line->zero = -1;
data[0].length = 255; /* see above */
data[0].value = 0; /* shouldn't matter */
data[1].length = 255;
data[1].value = 0;
data[1].nix = 2;
rcur = data + index;
for ( ; left > 0; index++, rcur++, left -= 255 )
{ rcur->length = min(left, 255);
rcur->value = 0;
rcur->nix = (index - 1) ^ (index + 1);
}
rcur->nix = index - 2;
data[0].nix = index - 1;
line->xcur = 0;
line->rpcur.ptr = data + 2;
line->rpcur.index = 2;
line->rpcur.next = data[2].nix ^ 1;
line->free = index;
for ( ; index < rdev->runs_per_line; ++index )
data[index].nix = index + 1;
data[index - 1].nix = 0;
if ( y >= rdev->umin && y < rdev->umax1 )
{ if ( y > (rdev->umin + rdev->umax1) >> 1 )
rdev->umax1 = y;
else
rdev->umin = y + 1;
}
}
/* Replace an interval of a line with a new value. This is the procedure */
/* that does all the interesting work. We assume the line has been */
/* initialized, and that 0 <= xo < xe <= dev->width. */
private int
run_fill_interval(run_line *line, int xo, int xe, run_value new)
{ run *data = line->data;
int xc = line->xcur;
run_ptr rpc;
run_index itemp;
int x0, x1;
run_ptr rp0;
rpc = line->rpcur;
/* Find the run that contains xo. */
if ( xo < xc )
{ while ( xo < xc )
rp_prev(rpc, data, rpc, itemp), xc -= rp_length(rpc);
}
else
{ while ( xo >= xc + rp_length(rpc) )
xc += rp_length(rpc), rp_next(rpc, data, rpc, itemp);
}
/*
* Skip runs above xo that already contain the new value.
* If the entire interval already has the correct value, exit.
* If we skip any such runs, set xo to just above them.
*/
for ( ; !rp_at_end(rpc) && rp_value(rpc) == new;
rp_next(rpc, data, rpc, itemp)
)
if ( (xo = xc += rp_length(rpc)) >= xe )
return 0;
x0 = xc, rp0 = rpc;
/* Find the run that contains xe-1. */
while ( xe > xc + rp_length(rpc) )
xc += rp_length(rpc), rp_next(rpc, data, rpc, itemp);
/*
* Skip runs below xe that already contain the new value.
* (We know that some run between xo and xe doesn't.)
* If we skip any such runs, set xe to just below them.
*/
while ( rp_prev(rpc, data, rpc, itemp), rp_value(rpc) == new )
xe = xc -= rp_length(rpc);
rp_next(rpc, data, rpc, itemp);
/*
* At this point, we know the following:
* x0 <= xo < x0 + rp_length(rp0).
* rp_value(rp0) != new.
* xc <= xe-1 < xc + rp_length(rpc).
* rp_value(rpc) != new.
* Note that rp0 and rpc may point to the same run.
*/
/*
* Check that we have enough free runs to do the replacement.
* In the worst case, where we have to split existing runs
* at both ends of the interval, two new runs are required.
* We just check for having at least two free runs, since this
* is simple and wastes at most 2 runs.
*/
if ( line->free == 0 || data[line->free].nix == 0 )
return_error(-1);
/* Split off any unaffected prefix of the run at rp0. */
if ( x0 < xo )
{ uint diff = xo - x0;
run_value v0 = rp_value(rp0);
run_ptr rpp;
rp_prev(rp0, data, rpp, itemp);
if ( rp_value(rpp) == v0 && rp_length(rpp) + diff <= 255 )
rp_length(rpp) += diff;
else
{ rp_insert_prev(rp0, data, line, rpp, itemp);
rp_length(rpp) = diff;
rp_value(rpp) = v0;
}
}
/* Split off any unaffected suffix of the run at rpc. */
x1 = xc + rp_length(rpc);
if ( x1 > xe )
{ uint diff = x1 - xe;
run_value vc = rp_value(rpc);
run_ptr rpn;
rp_next(rpc, data, rpn, itemp);
if ( rp_value(rpn) == vc && rp_length(rpn) + diff <= 255 )
rp_length(rpn) += diff;
else
{ rp_insert_next(rpc, data, line, rpn, itemp);
rp_length(rpn) = diff;
rp_value(rpn) = vc;
}
}
/* Delete all runs from rp0 through rpc. */
rp_prev(rp0, data, rp0, itemp);
{ run_ptr rpn, rpn2;
while ( rp0.next != rpc.next )
rp_delete_next(rp0, data, line, rpn, rpn2, itemp);
}
/*
* Finally, insert new runs with the new value.
* We need to check for one boundary case, namely,
* xo == x0 and the next lower run has the new value.
* (There's probably a way to structure the code just slightly
* differently to avoid this test.)
*/
{ uint left = xe - xo;
if ( xo == x0 && rp_value(rp0) == new &&
rp_length(rp0) + left <= 255
)
rp_length(rp0) += left;
else
{ /*
* If we need more than one run, we probably should
* divide up the length to create more runs with length
* less than 255 in order to improve the chances of
* a later merge, but we won't bother right now.
*/
do
{ run_ptr rpn;
rp_insert_next(rp0, data, line, rpn, itemp);
rp_length(rpn) = min(left, 255);
rp_value(rpn) = new;
}
while ( (left -= 255) > 0 );
}
}
return 0;
}
/* Replace a rectangle with a new value. */
private int
run_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
gx_color_index color)
{ int xe;
run_line *line;
int ny;
fit_fill(dev, x, y, w, h);
/*
* If the new value is 0 and the rectangle falls entirely within
* the uninitialized region that we're keeping track of,
* we can skip the entire operation.
*/
if ( (byte)color == 0 && y >= rdev->umin && y + h <= rdev->umax1 )
return 0;
xe = x + w;
for ( line = &rdev->lines[y], ny = h; ny > 0; ++line, --ny )
if ( (byte)color != line->zero )
{ if ( line->zero == 0 )
run_line_initialize(dev, y + h - ny);
run_fill_interval(line, x, xe, (byte)color);
}
return 0;
}
/* Get a fully expanded scan line. */
private int
run_get_bits(gx_device *dev, int y, byte *row, byte **actual_row)
{ const run_line *line = &rdev->lines[y];
const run *data = line->data;
const_run_ptr rp;
byte *q = *actual_row = row;
run_index itemp;
if ( line->zero == 0 )
{ memset(row, 0, dev->width);
return 0;
}
for ( rp_start(rp, data); !rp_at_end(rp);
rp_next(rp, data, rp, itemp)
)
{ memset(q, rp_value(rp), rp_length(rp));
q += rp_length(rp);
}
return 0;
}
/* Debugging code */
#ifdef DEBUG
void
debug_print_run(const run *data, run_index index, const char *prefix)
{ const run *pr = data + index;
dprintf5("%s%5d: length = %3d, value = %3d, nix = %5u\n",
prefix, index, pr->length, pr->value, pr->nix);
}
void
debug_print_run_line(const run_line *line, const char *prefix)
{ const run *data = line->data;
dprintf5("%sruns at 0x%lx: zero = %d, free = %u, xcur = %u,\n",
prefix, (ulong)data, line->zero, line->free, line->xcur);
dprintf4("%s rpcur = {ptr = 0x%lx, index = %u, next = %u}\n",
prefix, (ulong)line->rpcur.ptr, line->rpcur.index, line->rpcur.next);
{ const_run_ptr rpc;
uint itemp;
rp_start(rpc, data);
while ( !rp_at_end(rpc) )
{ debug_print_run(data, rpc.index, prefix);
rp_next(rpc, data, rpc, itemp);
}
}
}
#endif /* DEBUG */
|