]> www.fi.muni.cz Git - evince.git/blob - backend/dvi/mdvi-lib/color.c
366b0ea6814b984552b0b8fdab14293c79aee696
[evince.git] / backend / dvi / mdvi-lib / color.c
1 /*
2  * Copyright (C) 2000, Matias Atria
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #include "mdvi.h"
20 #include "color.h"
21
22 void    mdvi_set_color(DviContext *dvi, Ulong fg, Ulong bg)
23 {
24         if(dvi->curr_fg != fg || dvi->curr_bg != bg) {
25                 DEBUG((DBG_DEVICE, "setting color to (%lu,%lu)\n", fg, bg));
26                 if(dvi->device.set_color)
27                         dvi->device.set_color(dvi->device.device_data, fg, bg);
28                 dvi->curr_fg = fg;
29                 dvi->curr_bg = bg;
30         }
31 }
32
33 void    mdvi_push_color(DviContext *dvi, Ulong fg, Ulong bg)
34 {
35         if(dvi->color_top == dvi->color_size) {
36                 dvi->color_size += 32;
37                 dvi->color_stack = mdvi_realloc(dvi->color_stack,
38                         dvi->color_size * sizeof(DviColorPair));
39         }
40         dvi->color_stack[dvi->color_top].fg = dvi->curr_fg;
41         dvi->color_stack[dvi->color_top].bg = dvi->curr_bg;
42         dvi->color_top++;
43         mdvi_set_color(dvi, fg, bg);
44 }
45
46 void    mdvi_pop_color(DviContext *dvi)
47 {
48         Ulong   fg, bg;
49         
50         if(dvi->color_top == 0)
51                 return;
52         dvi->color_top--;
53         fg = dvi->color_stack[dvi->color_top].fg;
54         bg = dvi->color_stack[dvi->color_top].bg;
55         mdvi_set_color(dvi, fg, bg);
56 }
57
58 void    mdvi_reset_color(DviContext *dvi)
59 {
60         dvi->color_top = 0;
61         mdvi_set_color(dvi, dvi->params.fg, dvi->params.bg);
62 }
63
64 /* cache for color tables, to avoid creating them for every glyph */
65 typedef struct {
66         Ulong   fg;
67         Ulong   bg;
68         Uint    nlevels;
69         Ulong   *pixels;
70         int     density;
71         double  gamma;
72         Uint    hits;
73 } ColorCache;
74
75 #define CCSIZE          256
76 static ColorCache       color_cache[CCSIZE];
77 static int              cc_entries;
78
79 #define GAMMA_DIFF      0.005
80
81
82 /* create a color table */
83 Ulong   *get_color_table(DviDevice *dev, 
84                          int nlevels, Ulong fg, Ulong bg, double gamma, int density)
85 {
86         ColorCache      *cc, *tofree;
87         int             lohits;
88         Ulong           *pixels;
89         int             status;
90
91         lohits = color_cache[0].hits;
92         tofree = &color_cache[0];
93         /* look in the cache and see if we have one that matches this request */
94         for(cc = &color_cache[0]; cc < &color_cache[cc_entries]; cc++) {
95                 if(cc->hits < lohits) {
96                         lohits = cc->hits;
97                         tofree = cc;
98                 }
99                 if(cc->fg == fg && cc->bg == bg && cc->density == density &&
100                    cc->nlevels == nlevels && fabs(cc->gamma - gamma) <= GAMMA_DIFF)
101                         break;
102         }
103
104         if(cc < &color_cache[cc_entries]) {
105                 cc->hits++;
106                 return cc->pixels;
107         }
108
109         DEBUG((DBG_DEVICE, "Adding color table to cache (fg=%lu, bg=%lu, n=%d)\n",
110                 fg, bg, nlevels));
111                 
112         /* no entry was found in the cache, create a new one */
113         if(cc_entries < CCSIZE) {
114                 cc = &color_cache[cc_entries++];
115                 cc->pixels = NULL;
116         } else {
117                 cc = tofree;
118                 mdvi_free(cc->pixels);
119         }
120         pixels = xnalloc(Ulong, nlevels);
121         status = dev->alloc_colors(dev->device_data, 
122                 pixels, nlevels, fg, bg, gamma, density);
123         if(status < 0) {
124                 mdvi_free(pixels);
125                 return NULL;
126         }
127         cc->fg = fg;
128         cc->bg = bg;
129         cc->gamma = gamma;
130         cc->density = density;
131         cc->nlevels = nlevels;
132         cc->pixels = pixels;
133         cc->hits = 1;
134         return pixels;  
135 }