Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4680 | right-hear | 1 | #include "fitz.h" |
2 | |||
3 | fz_halftone * |
||
4 | fz_new_halftone(int comps) |
||
5 | { |
||
6 | fz_halftone *ht; |
||
7 | int i; |
||
8 | |||
9 | ht = fz_malloc(sizeof(fz_halftone) + (comps-1)*sizeof(fz_pixmap *)); |
||
10 | ht->refs = 1; |
||
11 | ht->n = comps; |
||
12 | for (i = 0; i < comps; i++) |
||
13 | ht->comp[i] = NULL; |
||
14 | |||
15 | return ht; |
||
16 | } |
||
17 | |||
18 | fz_halftone * |
||
19 | fz_keep_halftone(fz_halftone *ht) |
||
20 | { |
||
21 | ht->refs++; |
||
22 | return ht; |
||
23 | } |
||
24 | |||
25 | void |
||
26 | fz_drop_halftone(fz_halftone *ht) |
||
27 | { |
||
28 | int i; |
||
29 | |||
30 | if (!ht || --ht->refs != 0) |
||
31 | return; |
||
32 | for (i = 0; i < ht->n; i++) |
||
33 | fz_drop_pixmap(ht->comp[i]); |
||
34 | fz_free(ht); |
||
35 | } |
||
36 | |||
37 | /* Default mono halftone, lifted from Ghostscript. */ |
||
38 | static unsigned char mono_ht[] = |
||
39 | { |
||
40 | 0x0E, 0x8E, 0x2E, 0xAE, 0x06, 0x86, 0x26, 0xA6, 0x0C, 0x8C, 0x2C, 0xAC, 0x04, 0x84, 0x24, 0xA4, |
||
41 | 0xCE, 0x4E, 0xEE, 0x6E, 0xC6, 0x46, 0xE6, 0x66, 0xCC, 0x4C, 0xEC, 0x6C, 0xC4, 0x44, 0xE4, 0x64, |
||
42 | 0x3E, 0xBE, 0x1E, 0x9E, 0x36, 0xB6, 0x16, 0x96, 0x3C, 0xBC, 0x1C, 0x9C, 0x34, 0xB4, 0x14, 0x94, |
||
43 | 0xFE, 0x7E, 0xDE, 0x5E, 0xF6, 0x76, 0xD6, 0x56, 0xFC, 0x7C, 0xDC, 0x5C, 0xF4, 0x74, 0xD4, 0x54, |
||
44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x10, 0x90, 0x38, 0xB8, 0x18, 0x98, |
||
55 | 0xF2, 0x72, 0xD2, 0x52, 0xFA, 0x7A, 0xDA, 0x5A, 0xF0, 0x70, 0xD0, 0x50, 0xF8, 0x78, 0xD8, 0x58 |
||
56 | }; |
||
57 | |||
58 | fz_halftone *fz_get_default_halftone(int num_comps) |
||
59 | { |
||
60 | fz_halftone *ht = fz_new_halftone(num_comps); |
||
61 | assert(num_comps == 1); /* Only support 1 component for now */ |
||
62 | ht->comp[0] = fz_new_pixmap_with_data(NULL, 16, 16, mono_ht); |
||
63 | return ht; |
||
64 | } |
||
65 | |||
66 | /* Finally, code to actually perform halftoning. */ |
||
67 | static void make_ht_line(unsigned char *buf, fz_halftone *ht, int x, int y, int w) |
||
68 | { |
||
69 | /* FIXME: There is a potential optimisation here; in the case where |
||
70 | * the LCM of the halftone tile widths is smaller than w, we could |
||
71 | * form just one 'LCM' run, then copy it repeatedly. |
||
72 | */ |
||
73 | int k, n; |
||
74 | n = ht->n; |
||
75 | for (k = 0; k < n; k++) |
||
76 | { |
||
77 | fz_pixmap *tile = ht->comp[k]; |
||
78 | unsigned char *b = buf++; |
||
79 | unsigned char *t; |
||
80 | unsigned char *tbase; |
||
81 | int px = x + tile->x; |
||
82 | int py = y + tile->y; |
||
83 | int tw = tile->w; |
||
84 | int th = tile->h; |
||
85 | int w2 = w; |
||
86 | int len; |
||
87 | px = px % tw; |
||
88 | if (px < 0) |
||
89 | px += tw; |
||
90 | py = py % th; |
||
91 | if (py < 0) |
||
92 | py += th; |
||
93 | |||
94 | assert(tile->n == 1); |
||
95 | |||
96 | /* Left hand section; from x to tile width */ |
||
97 | tbase = tile->samples + py * tw; |
||
98 | t = tbase + px; |
||
99 | len = tw - px; |
||
100 | if (len > w2) |
||
101 | len = w2; |
||
102 | w2 -= len; |
||
103 | while (len--) |
||
104 | { |
||
105 | *b = *t++; |
||
106 | b += n; |
||
107 | } |
||
108 | |||
109 | /* Centre section - complete copies */ |
||
110 | w2 -= tw; |
||
111 | while (w2 >= 0) |
||
112 | { |
||
113 | len = tw; |
||
114 | t = tbase; |
||
115 | while (len--) |
||
116 | { |
||
117 | *b = *t++; |
||
118 | b += n; |
||
119 | } |
||
120 | w2 -= tw; |
||
121 | } |
||
122 | w2 += tw; |
||
123 | |||
124 | /* Right hand section - stragglers */ |
||
125 | t = tbase; |
||
126 | while (w2--) |
||
127 | { |
||
128 | *b = *t++; |
||
129 | b += n; |
||
130 | } |
||
131 | } |
||
132 | } |
||
133 | |||
134 | /* Inner mono thresholding code */ |
||
135 | static void do_threshold_1(unsigned char *ht_line, unsigned char *pixmap, unsigned char *out, int w) |
||
136 | { |
||
137 | int bit = 0x80; |
||
138 | int h = 0; |
||
139 | |||
140 | do |
||
141 | { |
||
142 | if (*pixmap < *ht_line++) |
||
143 | h |= bit; |
||
144 | pixmap += 2; /* Skip the alpha */ |
||
145 | bit >>= 1; |
||
146 | if (bit == 0) |
||
147 | { |
||
148 | *out++ = h; |
||
149 | h = 0; |
||
150 | bit = 0x80; |
||
151 | } |
||
152 | |||
153 | } |
||
154 | while (--w); |
||
155 | if (bit != 0x80) |
||
156 | *out++ = h; |
||
157 | } |
||
158 | |||
159 | fz_bitmap *fz_halftone_pixmap(fz_pixmap *pix, fz_halftone *ht) |
||
160 | { |
||
161 | fz_bitmap *out; |
||
162 | unsigned char *ht_line, *o, *p; |
||
163 | int w, h, x, y, n, pstride, ostride; |
||
164 | |||
165 | if (pix == NULL || ht == NULL) |
||
166 | return NULL; |
||
167 | |||
168 | assert(pix->n == 2); /* Mono + Alpha */ |
||
169 | |||
170 | n = pix->n-1; /* Remove alpha */ |
||
171 | ht_line = fz_malloc(pix->w * n); |
||
172 | out = fz_new_bitmap(pix->w, pix->h, n); |
||
173 | o = out->samples; |
||
174 | p = pix->samples; |
||
175 | |||
176 | h = pix->h; |
||
177 | x = pix->x; |
||
178 | y = pix->y; |
||
179 | w = pix->w; |
||
180 | ostride = out->stride; |
||
181 | pstride = pix->w * pix->n; |
||
182 | while (h--) |
||
183 | { |
||
184 | make_ht_line(ht_line, ht, x, y++, w); |
||
185 | do_threshold_1(ht_line, p, o, w); |
||
186 | o += ostride; |
||
187 | p += pstride; |
||
188 | } |
||
189 | return out; |
||
190 | }>>>>>> |