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
|
/* Copyright (C) 1994, 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 Ghostscript General Public License for full details.
*/
/* gdevm24.c */
/* 24-bit-per-pixel "memory" (stored bitmap) device */
#include "memory_.h"
#include "gx.h"
#include "gxdevice.h"
#include "gxdevmem.h" /* semi-public definitions */
#include "gdevmem.h" /* private definitions */
#undef chunk
#define chunk byte
/* Procedures */
declare_mem_procs(mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
/* The device descriptor. */
const gx_device_memory far_data mem_true24_color_device =
mem_device("image(24)", 24, 0,
gx_default_rgb_map_rgb_color, gx_default_rgb_map_color_rgb,
mem_true24_copy_mono, mem_true24_copy_color, mem_true24_fill_rectangle);
/* Convert x coordinate to byte offset in scan line. */
#undef x_to_byte
#define x_to_byte(x) ((x) * 3)
/* Unpack a color into its bytes. */
#define declare_unpack_color(r, g, b, color)\
byte r = (byte)(color >> 16);\
byte g = (byte)((uint)color >> 8);\
byte b = (byte)color
#if arch_is_big_endian
# define declare_pack_color(cword, rgb, r, g, b)\
bits32 cword = (bits32)(rgb) << 8
#else
# define declare_pack_color(cword, rgb, r, g, b)\
bits32 cword =\
((bits32)(b) << 16) | ((bits16)(g) << 8) | (r)
#endif
/* Put a 24-bit color into the bitmap. */
#define put3(ptr, r, g, b)\
(ptr)[0] = r, (ptr)[1] = g, (ptr)[2] = b
/* Put 4 bytes of color into the bitmap. */
#define putw(ptr, wxyz)\
*(bits32 *)(ptr) = (wxyz)
/* Load the 3-word 24-bit-color cache. */
/* Free variables: [m]dev, rgbr, gbrg, brgb. */
#if arch_is_big_endian
# define set_color24_cache(crgb, r, g, b)\
mdev->color24.rgbr = rgbr = ((bits32)(crgb) << 8) | (r),\
mdev->color24.gbrg = gbrg = (rgbr << 8) | (g),\
mdev->color24.brgb = brgb = (gbrg << 8) | (b),\
mdev->color24.rgb = (crgb)
#else
# define set_color24_cache(crgb, r, g, b)\
mdev->color24.rgbr = rgbr =\
((bits32)(r) << 24) | ((bits32)(b) << 16) |\
((bits16)(g) << 8) | (r),\
mdev->color24.brgb = brgb = (rgbr << 8) | (b),\
mdev->color24.gbrg = gbrg = (brgb << 8) | (g),\
mdev->color24.rgb = (crgb)
#endif
/* Fill a rectangle with a color. */
private int
mem_true24_fill_rectangle(gx_device *dev,
int x, int y, int w, int h, gx_color_index color)
{ declare_unpack_color(r, g, b, color);
declare_scan_ptr(dest);
fit_fill(dev, x, y, w, h);
setup_rect(dest);
if ( w >= 5 )
{ if ( r == g && r == b)
{
#if 1
/* We think we can do better than the library's memset.... */
int bcntm7 = w * 3 - 7;
register bits32 cword = color | (color << 24);
while ( h-- > 0 )
{ register byte *pptr = dest;
byte *limit = pptr + bcntm7;
/* We want to store full words, but we have to */
/* guarantee that they are word-aligned. */
switch ( x & 3 )
{
case 3: *pptr++ = (byte)cword;
case 2: *pptr++ = (byte)cword;
case 1: *pptr++ = (byte)cword;
case 0: ;
}
/* Even with w = 5, we always store at least */
/* 3 full words, regardless of the starting x. */
*(bits32 *)pptr =
((bits32 *)pptr)[1] =
((bits32 *)pptr)[2] = cword;
pptr += 12;
while ( pptr < limit )
{ *(bits32 *)pptr =
((bits32 *)pptr)[1] = cword;
pptr += 8;
}
switch ( pptr - limit )
{
case 0: pptr[6] = (byte)cword;
case 1: pptr[5] = (byte)cword;
case 2: pptr[4] = (byte)cword;
case 3: *(bits32 *)pptr = cword;
break;
case 4: pptr[2] = (byte)cword;
case 5: pptr[1] = (byte)cword;
case 6: pptr[0] = (byte)cword;
case 7: ;
}
inc_ptr(dest, draster);
}
#else
int bcnt = w * 3;
while ( h-- > 0 )
{ memset(dest, r, bcnt);
inc_ptr(dest, draster);
}
#endif
}
else
{ int x3 = -x & 3, ww = w - x3;
bits32 rgbr, gbrg, brgb;
if ( mdev->color24.rgb == color )
rgbr = mdev->color24.rgbr,
gbrg = mdev->color24.gbrg,
brgb = mdev->color24.brgb;
else
set_color24_cache(color, r, g, b);
while ( h-- > 0 )
{ register byte *pptr = dest;
int w1 = ww;
switch ( x3 )
{
case 1:
put3(pptr, r, g, b);
pptr += 3; break;
case 2:
pptr[0] = r; pptr[1] = g;
putw(pptr + 2, brgb);
pptr += 6; break;
case 3:
pptr[0] = r;
putw(pptr + 1, gbrg);
putw(pptr + 5, brgb);
pptr += 9; break;
case 0:
;
}
while ( w1 >= 4 )
{ putw(pptr, rgbr);
putw(pptr + 4, gbrg);
putw(pptr + 8, brgb);
pptr += 12;
w1 -= 4;
}
switch ( w1 )
{
case 1:
put3(pptr, r, g, b);
break;
case 2:
putw(pptr, rgbr);
pptr[4] = g; pptr[5] = b;
break;
case 3:
putw(pptr, rgbr);
putw(pptr + 4, gbrg);
pptr[8] = b;
break;
case 0:
;
}
inc_ptr(dest, draster);
}
}
}
else /* w < 5 */
switch ( w )
{
/* Note that fit_fill guarantees w > 0, h > 0. */
case 4:
do
{ dest[9] = dest[6] = dest[3] = dest[0] = r;
dest[10] = dest[7] = dest[4] = dest[1] = g;
dest[11] = dest[8] = dest[5] = dest[2] = b;
inc_ptr(dest, draster);
}
while ( --h );
break;
case 3:
do
{ dest[6] = dest[3] = dest[0] = r;
dest[7] = dest[4] = dest[1] = g;
dest[8] = dest[5] = dest[2] = b;
inc_ptr(dest, draster);
}
while ( --h );
break;
case 2:
do
{ dest[3] = dest[0] = r;
dest[4] = dest[1] = g;
dest[5] = dest[2] = b;
inc_ptr(dest, draster);
}
while ( --h );
break;
case 1:
do
{ dest[0] = r, dest[1] = g, dest[2] = b;
inc_ptr(dest, draster);
}
while ( --h );
break;
case 0:
;
}
return 0;
}
/* Copy a monochrome bitmap. */
private int
mem_true24_copy_mono(gx_device *dev,
const byte *base, int sourcex, int sraster, gx_bitmap_id id,
int x, int y, int w, int h, gx_color_index zero, gx_color_index one)
{ const byte *line;
int sbit;
int first_bit;
declare_scan_ptr(dest);
fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
setup_rect(dest);
line = base + (sourcex >> 3);
sbit = sourcex & 7;
first_bit = 0x80 >> sbit;
if ( zero != gx_no_color_index )
{ /* Loop for halftones or inverted masks */
/* (never used). */
declare_unpack_color(r0, g0, b0, zero);
declare_unpack_color(r1, g1, b1, one);
while ( h-- > 0 )
{ register byte *pptr = dest;
const byte *sptr = line;
register int sbyte = *sptr++;
register int bit = first_bit;
int count = w;
do
{ if ( sbyte & bit )
{ if ( one != gx_no_color_index )
put3(pptr, r1, g1, b1);
}
else
put3(pptr, r0, g0, b0);
pptr += 3;
if ( (bit >>= 1) == 0 )
bit = 0x80, sbyte = *sptr++;
}
while ( --count > 0 );
line += sraster;
inc_ptr(dest, draster);
}
}
else if ( one != gx_no_color_index )
{ /* Loop for character and pattern masks. */
/* This is used heavily. */
declare_unpack_color(r1, g1, b1, one);
int first_mask = first_bit << 1;
int first_count, first_skip;
if ( sbit + w > 8 )
first_mask -= 1,
first_count = 8 - sbit;
else
first_mask -= first_mask >> w,
first_count = w;
first_skip = first_count * 3;
while ( h-- > 0 )
{ register byte *pptr = dest;
const byte *sptr = line;
register int sbyte = *sptr++ & first_mask;
int count = w - first_count;
if ( sbyte )
{ register int bit = first_bit;
do
{ if ( sbyte & bit )
put3(pptr, r1, g1, b1);
pptr += 3;
}
while ( (bit >>= 1) & first_mask );
}
else
pptr += first_skip;
while ( count >= 8 )
{ sbyte = *sptr++;
if ( sbyte & 0xf0 )
{ if ( sbyte & 0x80 )
put3(pptr, r1, g1, b1);
if ( sbyte & 0x40 )
put3(pptr + 3, r1, g1, b1);
if ( sbyte & 0x20 )
put3(pptr + 6, r1, g1, b1);
if ( sbyte & 0x10 )
put3(pptr + 9, r1, g1, b1);
}
if ( sbyte & 0xf )
{ if ( sbyte & 8 )
put3(pptr + 12, r1, g1, b1);
if ( sbyte & 4 )
put3(pptr + 15, r1, g1, b1);
if ( sbyte & 2 )
put3(pptr + 18, r1, g1, b1);
if ( sbyte & 1 )
put3(pptr + 21, r1, g1, b1);
}
pptr += 24;
count -= 8;
}
if ( count > 0 )
{ register int bit = 0x80;
sbyte = *sptr++;
do
{ if ( sbyte & bit )
put3(pptr, r1, g1, b1);
pptr += 3;
bit >>= 1;
}
while ( --count > 0 );
}
line += sraster;
inc_ptr(dest, draster);
}
}
return 0;
}
/* Copy a color bitmap. */
private int
mem_true24_copy_color(gx_device *dev,
const byte *base, int sourcex, int sraster, gx_bitmap_id id,
int x, int y, int w, int h)
{ fit_copy(dev, base, sourcex, sraster, id, x, y, w, h);
mem_copy_byte_rect(mdev, base, sourcex, sraster, x, y, w, h);
return 0;
}
|