#include <stdlib.h>
#include "mdvi.h"
+#include "color.h"
/* bit_masks[n] contains a BmUnit with `n' contiguous bits */
#define SHOW_OP_DATA (DEBUGGING(BITMAP_OPS) && DEBUGGING(BITMAP_DATA))
#endif
-/* cache for color tables, to avoid creating them for every glyph */
-typedef struct {
- Ulong fg;
- Ulong bg;
- Uint nlevels;
- Ulong *pixels;
- int density;
- double gamma;
- Uint hits;
-} ColorCache;
-
/*
* Some useful macros to manipulate bitmap data
* SEGMENT(m,n) = bit mask for a segment of `m' contiguous bits
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff
};
-#define CCSIZE 256
-static ColorCache color_cache[CCSIZE];
-static int cc_entries;
-
-#define GAMMA_DIFF 0.005
-
-static Ulong *get_color_table(DviDevice *dev,
- int nlevels, Ulong fg, Ulong bg, double gamma, int density);
-
-
-/* create a color table */
-static Ulong *get_color_table(DviDevice *dev,
- int nlevels, Ulong fg, Ulong bg, double gamma, int density)
-{
- ColorCache *cc, *tofree;
- int lohits;
- Ulong *pixels;
- int status;
-
- lohits = color_cache[0].hits;
- tofree = &color_cache[0];
- /* look in the cache and see if we have one that matches this request */
- for(cc = &color_cache[0]; cc < &color_cache[cc_entries]; cc++) {
- if(cc->hits < lohits) {
- lohits = cc->hits;
- tofree = cc;
- }
- if(cc->fg == fg && cc->bg == bg && cc->density == density &&
- cc->nlevels == nlevels && fabs(cc->gamma - gamma) <= GAMMA_DIFF)
- break;
- }
-
- if(cc < &color_cache[cc_entries]) {
- cc->hits++;
- return cc->pixels;
- }
-
- DEBUG((DBG_DEVICE, "Adding color table to cache (fg=%lu, bg=%lu, n=%d)\n",
- fg, bg, nlevels));
-
- /* no entry was found in the cache, create a new one */
- if(cc_entries < CCSIZE) {
- cc = &color_cache[cc_entries++];
- cc->pixels = NULL;
- } else {
- cc = tofree;
- xfree(cc->pixels);
- }
- pixels = xnalloc(Ulong, nlevels);
- status = dev->alloc_colors(dev->device_data,
- pixels, nlevels, fg, bg, gamma, density);
- if(status < 0) {
- xfree(pixels);
- return NULL;
- }
- cc->fg = fg;
- cc->bg = bg;
- cc->gamma = gamma;
- cc->density = density;
- cc->nlevels = nlevels;
- cc->pixels = pixels;
- cc->hits = 1;
- return pixels;
-}
/*
* next we have three bitmap functions to convert bitmaps in LSB bit order
bm->height = h;
bm->stride = BM_BYTES_PER_LINE(bm);
if(h && bm->stride)
- bm->data = (BmUnit *)xcalloc(h, bm->stride);
+ bm->data = (BmUnit *)mdvi_calloc(h, bm->stride);
else
bm->data = NULL;
bm->height = h;
bm->stride = BM_BYTES_PER_LINE(bm);
if(h && bm->stride)
- bm->data = (BmUnit *)xmalloc(h * bm->stride);
+ bm->data = (BmUnit *)mdvi_malloc(h * bm->stride);
else
bm->data = NULL;
nb.width = bm->width;
nb.height = bm->height;
nb.stride = bm->stride;
- nb.data = xcalloc(bm->height, bm->stride);
+ nb.data = mdvi_calloc(bm->height, bm->stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, nb.width-1, 0);
}
DEBUG((DBG_BITMAP_OPS, "flip_horizontally (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
if(SHOW_OP_DATA)
bitmap_print(stderr, bm);
nb.width = bm->width;
nb.height = bm->height;
nb.stride = bm->stride;
- nb.data = xcalloc(bm->height, bm->stride);
+ nb.data = mdvi_calloc(bm->height, bm->stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, 0, nb.height-1);
}
DEBUG((DBG_BITMAP_OPS, "flip_vertically (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
if(SHOW_OP_DATA)
bitmap_print(stderr, bm);
nb.width = bm->width;
nb.height = bm->height;
nb.stride = bm->stride;
- nb.data = xcalloc(bm->height, bm->stride);
+ nb.data = mdvi_calloc(bm->height, bm->stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, nb.width-1, nb.height-1);
}
DEBUG((DBG_BITMAP_OPS, "flip_diagonally (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
if(SHOW_OP_DATA)
bitmap_print(stderr, bm);
nb.width = bm->height;
nb.height = bm->width;
nb.stride = BM_BYTES_PER_LINE(&nb);
- nb.data = xcalloc(nb.height, nb.stride);
+ nb.data = mdvi_calloc(nb.height, nb.stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, nb.width - 1, 0);
DEBUG((DBG_BITMAP_OPS, "rotate_clockwise (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
bm->width = nb.width;
bm->height = nb.height;
nb.width = bm->height;
nb.height = bm->width;
nb.stride = BM_BYTES_PER_LINE(&nb);
- nb.data = xcalloc(nb.height, nb.stride);
+ nb.data = mdvi_calloc(nb.height, nb.stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, 0, nb.height - 1);
DEBUG((DBG_BITMAP_OPS, "rotate_counter_clockwise (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
bm->width = nb.width;
bm->height = nb.height;
nb.width = bm->height;
nb.height = bm->width;
nb.stride = BM_BYTES_PER_LINE(&nb);
- nb.data = xcalloc(nb.height, nb.stride);
+ nb.data = mdvi_calloc(nb.height, nb.stride);
fptr = bm->data;
tptr = __bm_unit_ptr(&nb, nb.width-1, nb.height-1);
}
DEBUG((DBG_BITMAP_OPS, "flip_rotate_clockwise (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
bm->width = nb.width;
bm->height = nb.height;
nb.width = bm->height;
nb.height = bm->width;
nb.stride = BM_BYTES_PER_LINE(&nb);
- nb.data = xcalloc(nb.height, nb.stride);
+ nb.data = mdvi_calloc(nb.height, nb.stride);
fptr = bm->data;
tptr = nb.data;
DEBUG((DBG_BITMAP_OPS, "flip_rotate_counter_clockwise (%d,%d) -> (%d,%d)\n",
bm->width, bm->height, nb.width, nb.height));
- xfree(bm->data);
+ mdvi_free(bm->data);
bm->data = nb.data;
bm->width = nb.width;
bm->height = nb.height;