Debian Codegraph @

converter/other/fiasco/codec/decoder.c

raw
1 /*
2 * decode.c: Decoding of an image represented by a WFA
3 *
4 * Written by: Ullrich Hafner
5 * Michael Unger
6 *
7 * This file is part of FIASCO (Fractal Image And Sequence COdec)
8 * Copyright (C) 1994-2000 Ullrich Hafner
9 */
10
11 /*
12 * $Date: 2000/10/22 10:44:48 $
13 * $Author: hafner $
14 * $Revision: 5.3 $
15 * $State: Exp $
16 */
17
18 #include "pm_config.h"
19 #include "config.h"
20
21 #include <string.h>
22
23 #include "pm_c_util.h"
24
25 #include "types.h"
26 #include "macros.h"
27 #include "error.h"
28
29 #include "wfa.h"
30 #include "image.h"
31 #include "misc.h"
32 #include "motion.h"
33 #include "read.h"
34 #include "wfalib.h"
35 #include "decoder.h"
36
37 /*****************************************************************************
38
39 prototypes
40
41 *****************************************************************************/
42
43 static void
44 compute_state_images (unsigned frame_level, word_t **simg,
45 const u_word_t *offset, const wfa_t *wfa);
46 static void
47 free_state_images (unsigned max_level, bool_t color, word_t **state_image,
48 u_word_t *offset, const unsigned *root_state,
49 unsigned range_state, format_e format, const wfa_t *wfa);
50 static void
51 alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
52 const unsigned *root_state, unsigned range_state,
53 unsigned max_level, format_e format, const wfa_t *wfa);
54 static void
55 compute_actual_size (unsigned luminance_root,
56 unsigned *width, unsigned *height, const wfa_t *wfa);
57 static void
58 enlarge_image (int enlarge_factor, format_e format, unsigned y_root,
59 wfa_t *wfa);
60 static word_t *
61 duplicate_state_image (const word_t *domain, unsigned offset, unsigned level);
62
63 /*****************************************************************************
64
65 public code
66
67 *****************************************************************************/
68
69 video_t *
70 alloc_video (bool_t store_wfa)
71 /*
72 * Video struct constructor:
73 * Initialize video structure and allocate memory for current, past
74 * and future WFA if flag 'store_wfa' is TRUE.
75 *
76 * Return value:
77 * pointer to the new video structure
78 */
79 {
80 video_t *video = Calloc (1, sizeof (video_t));
81
82 video->future_display = -1;
83 video->display = 0;
84
85 video->future = video->sfuture = video->past
86 = video->frame = video->sframe = NULL;
87
88 if (store_wfa)
89 {
90 video->wfa = alloc_wfa (NO);
91 video->wfa_past = alloc_wfa (NO);
92 video->wfa_future = alloc_wfa (NO);
93 }
94 else
95 video->wfa = video->wfa_past = video->wfa_future = NULL;
96
97 return video;
98 }
99
100 void
101 free_video (video_t *video)
102 /*
103 * Video struct destructor:
104 * Free memory of given 'video' struct.
105 *
106 * No return value.
107 *
108 * Side effects:
109 * 'video' struct is discarded.
110 */
111 {
112 if (video->past)
113 free_image (video->past);
114 if (video->future)
115 free_image (video->future);
116 if (video->sfuture)
117 free_image (video->sfuture);
118 if (video->frame)
119 free_image (video->frame);
120 if (video->sframe)
121 free_image (video->sframe);
122 if (video->wfa)
123 free_wfa (video->wfa);
124 if (video->wfa_past)
125 free_wfa (video->wfa_past);
126 if (video->wfa_future)
127 free_wfa (video->wfa_future);
128
129 Free (video);
130 }
131
132 image_t *
133 get_next_frame (bool_t store_wfa, int enlarge_factor,
134 int smoothing, const char *reference_frame,
135 format_e format, video_t *video, dectimer_t *timer,
136 wfa_t *orig_wfa, bitfile_t *input)
137 /*
138 * Get next frame of the WFA 'video' from stream 'input'.
139 * 'orig_wfa' is the constant part of the WFA used by all frames.
140 * Depending on values of 'enlarge_factor' and 'smoothing' enlarge and
141 * smooth image, respectively.
142 * If 'store_wfa' is TRUE, then store WFA structure of reference frames
143 * (used by analysis tool xwfa).
144 * If 'reference_frame' is not NULL, then load image 'reference_frame'
145 * from disk.
146 * 'format' gives the color format to be used (either 4:2:0 or 4:4:4).
147 * If 'timer' is not NULL, then accumulate running time statistics.
148 *
149 * Return value:
150 * pointer to decoded frame
151 *
152 * Side effects:
153 * 'video' and 'timer' struct are modified.
154 */
155 {
156 image_t *frame = NULL; /* current frame */
157 image_t *sframe = NULL; /* current smoothed frame */
158 bool_t current_frame_is_future_frame = NO;
159
160 if (video->future_display == video->display)
161 {
162 /*
163 * Future frame is already computed since it has been used
164 * as reference frame. So just return the stored frame.
165 */
166 if (video->frame) /* discard current frame */
167 free_image (video->frame);
168 video->frame = video->future;
169 video->future = NULL;
170
171 if (video->sframe) /* discard current (smoothed) frame */
172 free_image (video->sframe);
173 video->sframe = video->sfuture;
174 video->sfuture = NULL;
175
176 if (store_wfa)
177 copy_wfa (video->wfa, video->wfa_future);
178
179 video->display++;
180
181 if (!store_wfa)
182 video->wfa = NULL;
183 }
184 else
185 {
186 do /* compute next frame(s) */
187 {
188 unsigned frame_number; /* current frame number */
189 clock_t ptimer;
190 unsigned int stop_timer [3];
191 wfa_t *tmp_wfa = NULL;
192
193 if (!store_wfa)
194 video->wfa = orig_wfa;
195 else
196 {
197 tmp_wfa = alloc_wfa (NO);
198 copy_wfa (tmp_wfa, video->wfa);
199 copy_wfa (video->wfa, orig_wfa);
200 }
201
202 /*
203 * First step: read WFA from disk
204 */
205 prg_timer (&ptimer, START);
206 frame_number = read_next_wfa (video->wfa, input);
207 stop_timer [0] = prg_timer (&ptimer, STOP);
208 if (timer)
209 {
210 timer->input [video->wfa->frame_type] += stop_timer [0];
211 timer->frames [video->wfa->frame_type]++;
212 }
213
214 /*
215 * Read reference frame from disk if required
216 * (i.e., 1st frame is of type B or P)
217 */
218 if (video->display == 0 && video->wfa->frame_type != I_FRAME)
219 {
220 if (!reference_frame)
221 error ("First frame is %c-frame but no "
222 "reference frame is given.",
223 video->wfa->frame_type == B_FRAME ? 'B' : 'P');
224
225 video->frame = read_image_file (reference_frame);
226 video->sframe = NULL;
227 }
228
229 /*
230 * Depending on current frame type update past and future frames
231 */
232 if (video->wfa->frame_type == I_FRAME)
233 {
234 if (video->past) /* discard past frame */
235 free_image (video->past);
236 video->past = NULL;
237 if (video->future) /* discard future frame */
238 free_image (video->future);
239 video->future = NULL;
240 if (video->sfuture) /* discard (smoothed) future frame */
241 free_image (video->sfuture);
242 video->sfuture = NULL;
243 if (video->frame) /* discard current frame */
244 free_image (video->frame);
245 video->frame = NULL;
246 if (video->sframe) /* discard current (smoothed) frame */
247 free_image (video->sframe);
248 video->sframe = NULL;
249 }
250 else if (video->wfa->frame_type == P_FRAME)
251 {
252 if (video->past) /* discard past frame */
253 free_image (video->past);
254 video->past = video->frame; /* past <- current frame */
255 video->frame = NULL;
256 if (video->sframe) /* discard current (smoothed) frame */
257 free_image (video->sframe);
258 video->sframe = NULL;
259 if (store_wfa)
260 copy_wfa (video->wfa_past, tmp_wfa);
261 if (video->future) /* discard future frame */
262 free_image (video->future);
263 video->future = NULL;
264 if (video->sfuture) /* discard (smoothed) future frame */
265 free_image (video->sfuture);
266 video->sfuture = NULL;
267 }
268 else /* B_FRAME */
269 {
270 if (current_frame_is_future_frame)
271 {
272 if (video->future) /* discard future frame */
273 free_image (video->future);
274 video->future = frame; /* future <- current frame */
275 if (video->sfuture) /* discard (smoothed) future frame */
276 free_image (video->sfuture);
277 video->sfuture = sframe; /* future <- current (smoothed) */
278 if (store_wfa)
279 copy_wfa (video->wfa_future, tmp_wfa);
280 if (video->frame) /* discard current frame */
281 free_image (video->frame);
282 video->frame = NULL;
283 if (video->sframe) /* discard current (smoothed) frame */
284 free_image (video->sframe);
285 video->sframe = NULL;
286 frame = NULL;
287 sframe = NULL;
288 }
289 else
290 {
291 if (video->wfa->wfainfo->B_as_past_ref == YES)
292 {
293 if (video->past) /* discard past frame */
294 free_image (video->past);
295 video->past = video->frame; /* past <- current frame */
296 video->frame = NULL;
297 if (video->sframe) /* discard current (smoothed) frame */
298 free_image (video->sframe);
299 video->sframe = NULL;
300 if (store_wfa)
301 copy_wfa (video->wfa_past, tmp_wfa);
302 }
303 else
304 {
305 if (video->frame) /* discard current */
306 free_image (video->frame);
307 video->frame = NULL;
308 if (video->sframe) /* discard current (smoothed) frame */
309 free_image (video->sframe);
310 video->sframe = NULL;
311 }
312 }
313 }
314 if (tmp_wfa)
315 free_wfa (tmp_wfa);
316
317 current_frame_is_future_frame = NO;
318 /*
319 * Second step: decode image
320 * Optionally enlarge image if specified by option 'enlarge_factor'.
321 */
322 {
323 unsigned orig_width, orig_height;
324
325 stop_timer [0] = stop_timer [1] = stop_timer [2] = 0;
326
327 enlarge_image (enlarge_factor, format,
328 (video->wfa->wfainfo->color
329 && format == FORMAT_4_2_0)
330 ? video->wfa->tree [video->wfa->tree [video->wfa->root_state][0]][0] : -1, video->wfa);
331
332 if (enlarge_factor > 0)
333 {
334 orig_width = video->wfa->wfainfo->width << enlarge_factor;
335 orig_height = video->wfa->wfainfo->height << enlarge_factor;
336 }
337 else
338 {
339 orig_width = video->wfa->wfainfo->width >> - enlarge_factor;
340 orig_height = video->wfa->wfainfo->height >> - enlarge_factor;
341 if (orig_width & 1)
342 orig_width++;
343 if (orig_height & 1)
344 orig_height++;
345 }
346
347 frame = decode_image (orig_width, orig_height, format,
348 timer != NULL ? stop_timer : NULL,
349 video->wfa);
350 if (timer)
351 {
352 timer->preprocessing [video->wfa->frame_type] += stop_timer [0];
353 timer->decoder [video->wfa->frame_type] += stop_timer [1];
354 timer->cleanup [video->wfa->frame_type] += stop_timer [2];
355 }
356 }
357
358 /*
359 * Third step: restore motion compensation
360 */
361 if (video->wfa->frame_type != I_FRAME)
362 {
363 prg_timer (&ptimer, START);
364 restore_mc (enlarge_factor, frame, video->past, video->future,
365 video->wfa);
366 stop_timer [0] = prg_timer (&ptimer, STOP);
367 if (timer)
368 timer->motion [video->wfa->frame_type] += stop_timer [0];
369 }
370
371 /*
372 * Fourth step: smooth image along partitioning borders
373 */
374 prg_timer (&ptimer, START);
375 if (smoothing < 0) /* smoothing not changed by user */
376 smoothing = video->wfa->wfainfo->smoothing;
377 if (smoothing > 0 && smoothing <= 100)
378 {
379 sframe = clone_image (frame);
380 smooth_image (smoothing, video->wfa, sframe);
381 }
382 else
383 sframe = NULL;
384
385 stop_timer [0] = prg_timer (&ptimer, STOP);
386 if (timer)
387 timer->smooth [video->wfa->frame_type] += stop_timer [0];
388
389 if (frame_number == video->display)
390 {
391 video->display++;
392 video->frame = frame;
393 video->sframe = sframe;
394 frame = NULL;
395 sframe = NULL;
396 }
397 else if (frame_number > video->display)
398 {
399 video->future_display = frame_number;
400 current_frame_is_future_frame = YES;
401 }
402
403 if (!store_wfa)
404 remove_states (video->wfa->basis_states, video->wfa);
405 } while (!video->frame);
406
407 if (!store_wfa)
408 video->wfa = NULL;
409 }
410
411 return video->sframe ? video->sframe : video->frame;
412 }
413
414 image_t *
415 decode_image (unsigned orig_width, unsigned orig_height, format_e format,
416 unsigned *dec_timer, const wfa_t *wfa)
417 /*
418 * Compute image which is represented by the given 'wfa'.
419 * 'orig_width'x'orig_height' gives the resolution of the image at
420 * coding time. Use 4:2:0 subsampling or 4:4:4 'format' for color images.
421 * If 'dec_timer' is given, accumulate running time statistics.
422 *
423 * Return value:
424 * pointer to decoded image
425 *
426 * Side effects:
427 * '*dectimer' is changed if 'dectimer' != NULL.
428 */
429 {
430 unsigned root_state [3]; /* root of bintree for each band */
431 unsigned width, height; /* computed image size */
432 image_t *frame; /* regenerated frame */
433 word_t **images; /* pointer to array of pointers
434 to state images */
435 u_word_t *offsets; /* pointer to array of state image
436 offsets */
437 unsigned max_level; /* max. level of state with approx. */
438 unsigned state;
439 clock_t ptimer;
440
441 prg_timer (&ptimer, START);
442
443 /*
444 * Compute root of bintree for each color band
445 */
446 if (wfa->wfainfo->color)
447 {
448 root_state [Y] = wfa->tree [wfa->tree [wfa->root_state][0]][0];
449 root_state [Cb] = wfa->tree [wfa->tree [wfa->root_state][0]][1];
450 root_state [Cr] = wfa->tree [wfa->tree [wfa->root_state][1]][0];
451 }
452 else
453 root_state [GRAY] = wfa->root_state;
454
455 /*
456 * Compute maximum level of a linear combination
457 */
458 for (max_level = 0, state = wfa->basis_states; state < wfa->states; state++)
459 if (isedge (wfa->into [state][0][0]) || isedge (wfa->into [state][1][0]))
460 max_level = MAX(max_level, wfa->level_of_state [state]);
461
462
463 /*
464 * Allocate frame buffer for decoded image
465 */
466 compute_actual_size (format == FORMAT_4_2_0 ? root_state [Y] : MAXSTATES,
467 &width, &height, wfa);
468 width = MAX(width, orig_width);
469 height = MAX(height, orig_height);
470 frame = alloc_image (width, height, wfa->wfainfo->color, format);
471
472 /*
473 * Allocate buffers for intermediate state images
474 */
475 if (wfa->wfainfo->color)
476 {
477 wfa->level_of_state [wfa->root_state] = 128;
478 wfa->level_of_state [wfa->tree[wfa->root_state][0]] = 128;
479 wfa->level_of_state [wfa->tree[wfa->root_state][1]] = 128;
480 }
481 alloc_state_images (&images, &offsets, frame, root_state, 0, max_level,
482 format, wfa);
483
484 if (dec_timer)
485 dec_timer [0] += prg_timer (&ptimer, STOP);
486
487 /*
488 * Decode all state images, forming the complete image.
489 */
490 prg_timer (&ptimer, START);
491 compute_state_images (max_level, images, offsets, wfa);
492 if (dec_timer)
493 dec_timer [1] += prg_timer (&ptimer, STOP);
494
495 /*
496 * Cleanup buffers used for intermediate state images
497 */
498 prg_timer (&ptimer, START);
499 free_state_images (max_level, frame->color, images, offsets, root_state, 0,
500 format, wfa);
501
502 /*
503 * Crop decoded image if the image size differs.
504 */
505 if (orig_width != width || orig_height != height)
506 {
507 frame->height = orig_height;
508 frame->width = orig_width;
509 if (orig_width != width)
510 {
511 color_e band; /* current color band */
512 word_t *src, *dst; /* source and destination pointers */
513 unsigned y; /* current row */
514
515 for (band = first_band (frame->color);
516 band <= last_band (frame->color); band++)
517 {
518 src = dst = frame->pixels [band];
519 for (y = orig_height; y; y--)
520 {
521 memmove (dst, src, orig_width * sizeof (word_t));
522 dst += orig_width;
523 src += width;
524 }
525 if (format == FORMAT_4_2_0 && band == Y)
526 {
527 orig_width >>= 1;
528 orig_height >>= 1;
529 width >>= 1;
530 }
531 }
532 }
533 }
534 if (dec_timer)
535 dec_timer [2] += prg_timer (&ptimer, STOP);
536
537 return frame;
538 }
539
540 image_t *
541 decode_state (unsigned state, unsigned level, wfa_t *wfa)
542 /*
543 * Decode 'state' image of 'wfa' at given 'level'.
544 *
545 * Return value.
546 * pointer to decoded state image
547 *
548 * Side effects:
549 * 'wfa' states > 'state' are removed.
550 */
551 {
552 word_t *domains [2];
553 image_t *img = Calloc (1, sizeof (image_t));
554
555 /*
556 * Generate a new state with a 1.0 transition to 'state'
557 */
558 remove_states (state + 1, wfa);
559 append_edge (state + 1, state, 1.0, 0, wfa);
560 wfa->states = state + 2;
561
562 img->color = NO;
563 img->width = width_of_level (level);
564 img->height = height_of_level (level);
565 img->format = FORMAT_4_4_4;
566 img->pixels [GRAY] = decode_range (state + 1, 0, level, domains, wfa);
567
568 /*
569 * Copy decoded range to the frame buffer
570 */
571 {
572 word_t *src, *dst;
573 unsigned y;
574
575 src = domains [0];
576 dst = img->pixels [GRAY];
577 for (y = img->height; y; y--)
578 {
579 memcpy (dst, src, width_of_level (level) * sizeof (word_t));
580 src += width_of_level (level);
581 dst += img->width;
582 }
583 Free (domains [0]);
584 }
585
586 return img;
587 }
588
589 word_t *
590 decode_range (unsigned range_state, unsigned range_label, unsigned range_level,
591 word_t **domain, wfa_t *wfa)
592 /*
593 * Compute 'wfa' image of range (identified by 'state' and 'label')
594 * at 'range_level (works as function decode_image()).
595 *
596 * Return value:
597 * pointer to the pixels in SHORT format
598 *
599 * Side effects:
600 * if 'domain' != NULL then also the domain blocks
601 * of the corresponding range blocks are generated
602 * and returned in domain[]
603 * 'wfa->level_of_state []' is changed
604 */
605 {
606 image_t *state_image; /* regenerated state image */
607 word_t **images; /* pointer to array of pointers
608 to state images */
609 u_word_t *offsets; /* pointer to array of state image
610 offsets */
611 word_t *range;
612
613 enlarge_image (range_level - (wfa->level_of_state [range_state] - 1),
614 FORMAT_4_4_4, -1, wfa);
615 state_image = alloc_image (width_of_level (range_level + 1),
616 height_of_level (range_level + 1),
617 NO, FORMAT_4_4_4);
618 alloc_state_images (&images, &offsets, state_image, NULL, range_state,
619 range_level + 1, FORMAT_4_4_4, wfa);
620 compute_state_images (range_level + 1, images, offsets, wfa);
621
622 range = Calloc (size_of_level (range_level), sizeof (word_t));
623
624 if ((range_level & 1) == 0) /* square image */
625 {
626 memcpy (range,
627 images [range_state + (range_level + 1) * wfa->states]
628 + range_label * size_of_level (range_level),
629 size_of_level (range_level) * sizeof (word_t));
630 }
631 else /* rectangle */
632 {
633 word_t *src, *dst;
634 unsigned y;
635
636 src = images [range_state + (range_level + 1) * wfa->states]
637 + range_label * width_of_level (range_level);
638 dst = range;
639 for (y = height_of_level (range_level); y; y--)
640 {
641 memcpy (dst, src, width_of_level (range_level) * sizeof (word_t));
642 dst += width_of_level (range_level);
643 src += width_of_level (range_level + 1);
644 }
645 }
646
647 if (domain != NULL) /* copy domain images */
648 {
649 int s; /* domain state */
650 unsigned edge; /* counter */
651
652 if (ischild (s = wfa->tree [range_state][range_label]))
653 *domain++ = duplicate_state_image (images [s + (range_level)
654 * wfa->states],
655 offsets [s + (range_level)
656 * wfa->states],
657 range_level);
658 for (edge = 0; isedge (s = wfa->into[range_state][range_label][edge]);
659 edge++)
660 *domain++ = duplicate_state_image (images [s + (range_level)
661 * wfa->states],
662 offsets [s + (range_level)
663 * wfa->states],
664 range_level);
665 *domain = NULL;
666 }
667
668 free_state_images (range_level + 1, NO, images, offsets, NULL, range_state,
669 FORMAT_4_4_4, wfa);
670 free_image (state_image);
671
672 return range;
673 }
674
675 void
676 smooth_image (unsigned sf, const wfa_t *wfa, image_t *image)
677 /*
678 * Smooth 'image' along the partitioning boundaries of the 'wfa'
679 * with factor 's'.
680 *
681 * No return value.
682 *
683 * Side effects:
684 * pixel values of the 'image' are modified with respect to 's'
685 */
686 {
687 int is, inegs; /* integer factors of s and 1 - s*/
688 unsigned state;
689 unsigned img_width = image->width;
690 unsigned img_height = image->height;
691 real_t s = 1.0 - sf / 200.0;
692
693 if (s < 0.5 || s >= 1) /* value out of range */
694 return;
695
696 is = s * 512 + .5; /* integer representation of s */
697 inegs = (1 - s) * 512 + .5; /* integer representation of 1 - s */
698
699 for (state = wfa->basis_states;
700 state < (wfa->wfainfo->color
701 ? wfa->tree [wfa->root_state][0]
702 : wfa->states); state++)
703 {
704 word_t *bptr = image->pixels [Y]; /* pointer to right or
705 lower line */
706 unsigned level = wfa->level_of_state[state]; /* level of state image */
707 unsigned width = width_of_level (level); /* size of state image */
708 unsigned height = height_of_level (level); /* size of state image */
709
710 if (wfa->y [state][1] >= img_height || wfa->x [state][1] >= img_width)
711 continue; /* outside visible area */
712
713 if (level % 2) /* horizontal smoothing */
714 {
715 unsigned i; /* line counter */
716 word_t *img1; /* pointer to left or upper line */
717 word_t *img2; /* pointer to right or lower line */
718
719 img1 = bptr + (wfa->y [state][1] - 1) * img_width
720 + wfa->x [state][1];
721 img2 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1];
722
723 for (i = MIN(width, img_width - wfa->x [state][1]); i;
724 i--, img1++, img2++)
725 {
726 int tmp = *img1;
727
728 #ifdef HAVE_SIGNED_SHIFT
729 *img1 = (((is * tmp) >> 10) << 1)
730 + (((inegs * (int) *img2) >> 10) << 1);
731 *img2 = (((is * (int) *img2) >> 10) << 1)
732 + (((inegs * tmp) >> 10) << 1);
733 #else /* not HAVE_SIGNED_SHIFT */
734 *img1 = (((is * tmp) / 1024) * 2)
735 + (((inegs * (int) *img2) / 1024) * 2);
736 *img2 = (((is * (int) *img2) / 1024) * 2)
737 + (((inegs * tmp) / 1024) *2);
738 #endif /* not HAVE_SIGNED_SHIFT */
739 }
740 }
741 else /* vertical smoothing */
742 {
743 unsigned i; /* line counter */
744 word_t *img1; /* pointer to left or upper line */
745 word_t *img2; /* pointer to right or lower line */
746
747 img1 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1] - 1;
748 img2 = bptr + wfa->y [state][1] * img_width + wfa->x [state][1];
749
750 for (i = MIN(height, img_height - wfa->y [state][1]); i;
751 i--, img1 += img_width, img2 += img_width)
752 {
753 int tmp = *img1;
754
755 #ifdef HAVE_SIGNED_SHIFT
756 *img1 = (((is * tmp) >> 10) << 1)
757 + (((inegs * (int) *img2) >> 10) << 1);
758 *img2 = (((is * (int) *img2) >> 10) << 1)
759 + (((inegs * tmp) >> 10) << 1);
760 #else /* not HAVE_SIGNED_SHIFT */
761 *img1 = (((is * tmp) / 1024) * 2)
762 + (((inegs * (int) *img2) / 1024) * 2);
763 *img2 = (((is * (int) *img2) / 1024) * 2)
764 + (((inegs * tmp) / 1024) *2);
765 #endif /* not HAVE_SIGNED_SHIFT */
766 }
767 }
768 }
769 }
770
771 /*****************************************************************************
772
773 private code
774
775 *****************************************************************************/
776
777 static void
778 enlarge_image (int enlarge_factor, format_e format, unsigned y_root,
779 wfa_t *wfa)
780 /*
781 * Enlarge or reduce size of state images by factor 2^'enlarge_factor'.
782 * Use 4:2:0 subsampling if specified by 'format', else use 4:4:4 format.
783 * 'wfa' root state of the first chroma band is given by 'y_root' + 1.
784 *
785 * No return value.
786 *
787 * Side effects:
788 * coordinates of ranges and motion blocks in the WFA structure 'wfa'
789 * are modified.
790 */
791 {
792
793 if (enlarge_factor != 0 || format == FORMAT_4_2_0)
794 {
795 unsigned state;
796
797 if (enlarge_factor == 0)
798 {
799 state = y_root + 1;
800 enlarge_factor = -1;
801 }
802 else
803 state = wfa->basis_states;
804
805 for (; state < wfa->states; state++)
806 {
807 unsigned label, n;
808
809 wfa->level_of_state [state]
810 = MAX(wfa->level_of_state [state] + enlarge_factor * 2, 0);
811
812 for (label = 0; label < MAXLABELS; label++)
813 if (enlarge_factor > 0)
814 {
815 wfa->x [state][label] <<= enlarge_factor;
816 wfa->y [state][label] <<= enlarge_factor;
817 for (n = enlarge_factor; n; n--)
818 {
819 wfa->mv_tree [state][label].fx *= 2;
820 wfa->mv_tree [state][label].fy *= 2;
821 wfa->mv_tree [state][label].bx *= 2;
822 wfa->mv_tree [state][label].by *= 2;
823 }
824 }
825 else /* enlarge_factor < 0 */
826 {
827 wfa->x [state][label] >>= - enlarge_factor;
828 wfa->y [state][label] >>= - enlarge_factor;
829 for (n = - enlarge_factor; n; n--)
830 {
831 wfa->mv_tree [state][label].fx /= 2;
832 wfa->mv_tree [state][label].fy /= 2;
833 wfa->mv_tree [state][label].bx /= 2;
834 wfa->mv_tree [state][label].by /= 2;
835 }
836 }
837 if (format == FORMAT_4_2_0 && state == y_root)
838 enlarge_factor--;
839 }
840 }
841 }
842
843 static void
844 compute_actual_size (unsigned luminance_root,
845 unsigned *width, unsigned *height, const wfa_t *wfa)
846 /*
847 * Compute actual size of the frame represented by the given 'wfa'.
848 * (The reconstructed frame may get larger than the original due
849 * to the bintree partitioning.)
850 * If 'luminance_root' < MAXSTATES then the size of chroma ranges (4:2:0).
851 *
852 * Return values:
853 * actual 'width' and 'height' of the decoded frame.
854 */
855 {
856 unsigned x = 0, y = 0; /* maximum coordinates */
857 unsigned state; /* counter */
858
859 for (state = wfa->basis_states; state < wfa->states; state++)
860 if (isedge (wfa->into [state][0][0]) || isedge (wfa->into [state][1][0]))
861 {
862 unsigned mult = state > luminance_root ? 2 : 1;
863
864 x = MAX((wfa->x [state][0]
865 + width_of_level (wfa->level_of_state [state])) * mult, x);
866 y = MAX((wfa->y [state][0]
867 + height_of_level (wfa->level_of_state [state])) * mult, y);
868 }
869
870 if (x & 1) /* ensure that image size is even */
871 x++;
872 if (y & 1)
873 y++;
874 *width = x;
875 *height = y;
876 }
877
878 static void
879 alloc_state_images (word_t ***images, u_word_t **offsets, const image_t *frame,
880 const unsigned *root_state, unsigned range_state,
881 unsigned max_level, format_e format, const wfa_t *wfa)
882 /*
883 * Generate list of 'wfa' state images which have to be computed for
884 * each level to obtain the decoded 'frame'. 'root_state[]' denotes the
885 * state images of the three color bands.
886 * 'max_level' fives the max. level of a linear combination.
887 * Memory is allocated for every required state image.
888 * Use 4:2:0 subsampling or 4:4:4 'format' for color images.
889 * If 'range_state' > 0 then rather compute image of 'range_state' than
890 * image of 'wfa->root_state'.
891 *
892 * Return values:
893 * '*images' Pointer to array of state image pointers
894 * '*offsets' Pointer to array of state image offsets.
895 *
896 * Side effects:
897 * The arrays given above are filled with useful values.
898 */
899 {
900 word_t **simg; /* ptr to list of state image ptr's */
901 u_word_t *offs; /* ptr to list of offsets */
902 unsigned level; /* counter */
903
904 simg = Calloc (wfa->states * (max_level + 1), sizeof (word_t *));
905 offs = Calloc (wfa->states * (max_level + 1), sizeof (u_word_t));
906
907 /*
908 * Initialize buffers for those state images which are at 'max_level'.
909 */
910 if (range_state > 0) /* a range is given */
911 {
912 simg [range_state + max_level * wfa->states] = frame->pixels [GRAY];
913 offs [range_state + max_level * wfa->states] = frame->width;
914 }
915 else
916 {
917 unsigned state;
918
919 for (state = wfa->basis_states; state <= root_state [Y]; state++)
920 if (wfa->level_of_state [state] == max_level)
921 {
922 simg [state + max_level * wfa->states]
923 = (frame->pixels [Y] + wfa->y [state][0] * frame->width
924 + wfa->x [state][0]);
925 offs [state + max_level * wfa->states] = frame->width;
926 }
927 if (frame->color)
928 {
929 unsigned width = format == FORMAT_4_2_0 ?
930 (frame->width >> 1) : frame->width;
931 for (; state < wfa->states; state++)
932 if (wfa->level_of_state [state] == max_level)
933 {
934 simg [state + max_level * wfa->states]
935 = (frame->pixels [state > root_state [Cb] ? Cr : Cb]
936 + wfa->y [state][0] * width + wfa->x [state][0]);
937 offs [state + max_level * wfa->states] = width;
938 }
939 }
940 }
941
942 /*
943 * Generate list of state images which must be computed at each level
944 */
945 for (level = max_level; level > 0; level--)
946 {
947 int child, domain;
948 unsigned state, label, edge;
949
950 /*
951 * Range approximation with child.
952 */
953 for (state = 1; state < (range_state > 0 ?
954 range_state + 1 : wfa->states); state++)
955 if (simg [state + level * wfa->states])
956 for (label = 0; label < MAXLABELS; label++)
957 if (ischild (child = wfa->tree[state][label]))
958 {
959 if (isedge (wfa->into[state][label][0]))
960 {
961 /*
962 * Allocate new image block.
963 */
964 simg [child + (level - 1) * wfa->states]
965 = Calloc (size_of_level (level - 1), sizeof (word_t));
966 offs [child + (level - 1) * wfa->states]
967 = width_of_level (level - 1);
968 }
969 else
970 {
971 /*
972 * Use image block and offset of parent.
973 */
974 if (level & 1) /* split vertically */
975 {
976 simg [child + (level - 1) * wfa->states]
977 = (simg [state + level * wfa->states]
978 + label * (height_of_level (level - 1)
979 * offs [state
980 + level * wfa->states]));
981 }
982 else /* split horizontally */
983 {
984 simg [child + (level - 1) * wfa->states]
985 = (simg [state + level * wfa->states]
986 + label * width_of_level (level - 1));
987 }
988 offs [child + (level - 1) * wfa->states]
989 = offs [state + level * wfa->states];
990 }
991 }
992 /*
993 * Range approximation with linear combination
994 */
995 for (state = 1; state < (range_state > 0 ?
996 range_state + 1 : wfa->states); state++)
997 if (simg [state + level * wfa->states])
998 for (label = 0; label < MAXLABELS; label++)
999 for (edge = 0; isedge (domain = wfa->into[state][label][edge]);
1000 edge++)
1001 {
1002 if (domain > 0 /* don't allocate memory for state 0 */
1003 && !simg [domain + (level - 1) * wfa->states])
1004 {
1005 simg [domain + (level - 1) * wfa->states]
1006 = Calloc (size_of_level (level - 1), sizeof (word_t));
1007 offs [domain + (level - 1) * wfa->states]
1008 = width_of_level (level - 1);
1009 }
1010 }
1011
1012 }
1013
1014 *images = simg;
1015 *offsets = offs;
1016 }
1017
1018 static void
1019 free_state_images (unsigned max_level, bool_t color, word_t **state_image,
1020 u_word_t *offset, const unsigned *root_state,
1021 unsigned range_state, format_e format, const wfa_t *wfa)
1022 /*
1023 * Free memory of state images.
1024 * For more details refer to the inverse function 'alloc_state_images()'.
1025 *
1026 * No return value.
1027 *
1028 * Side effects:
1029 * arrays 'state_image' and 'offset' are discarded.
1030 */
1031 {
1032 word_t marker; /* ptr is required as a marker */
1033 unsigned level;
1034
1035 if (range_state > 0)
1036 {
1037 state_image [range_state + max_level * wfa->states] = &marker;
1038 }
1039 else
1040 {
1041 unsigned state;
1042
1043 /*
1044 * Initialize state image array with states at 'max_level'
1045 */
1046 for (state = wfa->basis_states; state <= root_state [Y]; state++)
1047 if (wfa->level_of_state [state] == max_level)
1048 state_image [state + max_level * wfa->states] = &marker;
1049
1050 if (color)
1051 {
1052 if (format == FORMAT_4_2_0)
1053 level = max_level - 2;
1054 else
1055 level = max_level;
1056
1057 for (; state < wfa->states; state++)
1058 if (wfa->level_of_state [state] == level)
1059 state_image [state + level * wfa->states] = &marker;
1060 }
1061 }
1062
1063 for (level = max_level; level > 0; level--)
1064 {
1065 int domain, child;
1066 unsigned state, label, edge;
1067 /*
1068 * Range approximation with child.
1069 */
1070 for (state = 1; state < (range_state > 0 ?
1071 range_state + 1 : wfa->states); state++)
1072 if (state_image [state + level * wfa->states])
1073 for (label = 0; label < MAXLABELS; label++)
1074 if (ischild (child = wfa->tree[state][label]))
1075 {
1076 if (isedge (wfa->into[state][label][0])
1077 && (state_image [child + (level - 1) * wfa->states]
1078 != &marker))
1079 Free (state_image [child + (level - 1) * wfa->states]);
1080 state_image [child + (level - 1) * wfa->states] = &marker;
1081 }
1082 /*
1083 * Range approximation with linear combination
1084 */
1085 for (state = 1; state < (range_state > 0 ?
1086 range_state + 1 : wfa->states);
1087 state++)
1088 if (state_image [state + level * wfa->states])
1089 for (label = 0; label < MAXLABELS; label++)
1090 for (edge = 0; isedge (domain = wfa->into[state][label][edge]);
1091 edge++)
1092 if (domain > 0
1093 && (state_image [domain + (level - 1) * wfa->states]
1094 != NULL)
1095 && (state_image [domain + (level - 1) * wfa->states]
1096 != &marker))
1097 {
1098 Free (state_image [domain + (level - 1) * wfa->states]);
1099 state_image [domain + (level - 1) * wfa->states]
1100 = &marker;
1101 }
1102 }
1103 Free (state_image);
1104 Free (offset);
1105 }
1106
1107 static void
1108 compute_state_images (unsigned max_level, word_t **simg,
1109 const u_word_t *offset, const wfa_t *wfa)
1110 /*
1111 * Compute all state images of the 'wfa' at level {1, ... , 'max_level'}
1112 * which are marked in the array 'simg' (offsets of state images
1113 * are given by 'offset').
1114 *
1115 * Warning: Several optimizations are used in this function making
1116 * it difficult to understand.
1117 *
1118 * No return value.
1119 *
1120 * Side effects:
1121 * state images (given by pointers in the array 'state_image')
1122 * are computed.
1123 */
1124 {
1125 unsigned level, state;
1126
1127 /*
1128 * Copy one-pixel images in case state_image pointer != &final distr.
1129 */
1130
1131 for (state = 1; state < wfa->states; state++)
1132 if (simg [state] != NULL) /* compute image at level 0 */
1133 *simg [state] = (int) (wfa->final_distribution[state] * 8 + .5) * 2;
1134
1135 /*
1136 * Compute images of states
1137 * Integer arithmetics are used rather than floating point operations.
1138 * 'weight' gives the weight in integer notation
1139 * 'src', 'dst', and 'idst' are pointers to the source and
1140 * destination pixels (short or integer format), respectively.
1141 * Short format : one operation per register (16 bit mode).
1142 * Integer format : two operations per register (32 bit mode).
1143 * 'src_offset', 'dst_offset', and 'dst_offset' give the number of
1144 * pixels which have to be omitted when jumping to the next image row.
1145 */
1146 for (level = 1; level <= max_level; level++)
1147 {
1148 unsigned label;
1149 unsigned width = width_of_level (level - 1);
1150 unsigned height = height_of_level (level - 1);
1151
1152 for (state = 1; state < wfa->states; state++)
1153 if (simg [state + level * wfa->states] != NULL)
1154 for (label = 0; label < MAXLABELS; label++)
1155 if (isedge (wfa->into [state][label][0]))
1156 {
1157 unsigned edge;
1158 int domain;
1159 word_t *range; /* address of current range */
1160 bool_t prediction_used; /* ND prediction found ? */
1161
1162 /*
1163 * Compute address of range image
1164 */
1165 if (level & 1) /* split vertically */
1166 {
1167 range = simg [state + level * wfa->states]
1168 + label * (height_of_level (level - 1)
1169 * offset [state
1170 + level * wfa->states]);
1171 }
1172 else /* split horizontally */
1173 {
1174 range = simg [state + level * wfa->states]
1175 + label * width_of_level (level - 1);
1176 }
1177
1178 /*
1179 * Generate the state images by adding the corresponding
1180 * weighted state images:
1181 * subimage [label] =
1182 * weight_1 * image_1 + ... + weight_n * image_n
1183 */
1184 if (!ischild (domain = wfa->tree[state][label]))
1185 prediction_used = NO;
1186 else
1187 {
1188 unsigned y;
1189 word_t *src;
1190 word_t *dst;
1191 unsigned src_offset;
1192 unsigned dst_offset;
1193
1194 prediction_used = YES;
1195 /*
1196 * Copy child image
1197 */
1198 src = simg [domain + (level - 1) * wfa->states];
1199 src_offset = offset [domain + (level - 1) * wfa->states] ;
1200 dst = range;
1201 dst_offset = offset [state + level * wfa->states];
1202 for (y = height; y; y--)
1203 {
1204 memcpy (dst, src, width * sizeof (word_t));
1205 src += src_offset;
1206 dst += dst_offset;
1207 }
1208 }
1209
1210 if (!prediction_used
1211 && isedge (domain = wfa->into[state][label][0]))
1212 {
1213 /*
1214 * If prediction is not used then the range is
1215 * filled with the first domain. No addition is needed.
1216 */
1217 edge = 0;
1218 if (domain != 0)
1219 {
1220 int weight;
1221 word_t *src;
1222 unsigned src_offset;
1223
1224 src = simg [domain + ((level - 1)
1225 * wfa->states)];
1226 src_offset = offset [domain + ((level - 1)
1227 * wfa->states)] - width;
1228 weight = wfa->int_weight [state][label][edge];
1229
1230 if (width == 1) /* can't add two-pixels in a row */
1231 {
1232 word_t *dst;
1233 unsigned dst_offset;
1234
1235 dst = range;
1236 dst_offset = offset [state + level * wfa->states]
1237 - width;
1238 #ifdef HAVE_SIGNED_SHIFT
1239 *dst++ = ((weight * (int) *src++) >> 10) << 1;
1240 #else /* not HAVE_SIGNED_SHIFT */
1241 *dst++ = ((weight * (int) *src++) / 1024) * 2;
1242 #endif /* not HAVE_SIGNED_SHIFT */
1243 if (height == 2)
1244 {
1245 src += src_offset;
1246 dst += dst_offset;
1247 #ifdef HAVE_SIGNED_SHIFT
1248 *dst++ = ((weight * (int) *src++) >> 10) << 1;
1249 #else /* not HAVE_SIGNED_SHIFT */
1250 *dst++ = ((weight * (int) *src++) / 1024) * 2;
1251 #endif /* not HAVE_SIGNED_SHIFT */
1252 }
1253 }
1254 else
1255 {
1256 unsigned y;
1257 int *idst;
1258 unsigned idst_offset;
1259
1260 idst = (int *) range;
1261 idst_offset = (offset [state + level * wfa->states]
1262 - width) / 2;
1263 for (y = height; y; y--)
1264 {
1265 int *comp_dst = idst + (width >> 1);
1266
1267 for (; idst != comp_dst; )
1268 {
1269 int tmp; /* temp. value of adjacent pixels */
1270 #ifdef HAVE_SIGNED_SHIFT
1271 # if BYTE_ORDER == LITTLE_ENDIAN
1272 tmp = (((weight * (int) src [1]) >> 10) << 17)
1273 | (((weight * (int) src [0]) >> 9)
1274 & 0xfffe);
1275 # else
1276 tmp = (((weight * (int) src [0]) >> 10) << 17)
1277 | (((weight * (int) src [1]) >> 9)
1278 & 0xfffe);
1279 # endif
1280 #else /* not HAVE_SIGNED_SHIFT */
1281 # if BYTE_ORDER == LITTLE_ENDIAN
1282 tmp = (((weight * (int) src [1]) / 1024)
1283 * 131072)
1284 | (((weight * (int) src [0])/ 512)
1285 & 0xfffe);
1286 # else
1287 tmp = (((weight * (int) src [0]) / 1024)
1288 * 131072)
1289 | (((weight * (int) src [1]) / 512)
1290 & 0xfffe);
1291 # endif
1292 #endif /* not HAVE_SIGNED_SHIFT */
1293 src += 2;
1294 *idst++ = tmp & 0xfffefffe;
1295 }
1296 src += src_offset;
1297 idst += idst_offset;
1298 }
1299 }
1300 }
1301 else
1302 {
1303 int weight = (int) (wfa->weight[state][label][edge]
1304 * wfa->final_distribution[0]
1305 * 8 + .5) * 2;
1306 /*
1307 * Range needs domain 0
1308 * (the constant function f(x, y) = 1),
1309 * hence a faster algorithm is used.
1310 */
1311 if (width == 1) /* can't add two-pixels in a row */
1312 {
1313 word_t *dst;
1314 unsigned dst_offset;
1315
1316 dst = range;
1317 dst_offset = offset [state + level * wfa->states]
1318 - width;
1319
1320 *dst++ = weight;
1321 if (height == 2)
1322 {
1323 dst += dst_offset;
1324 *dst++ = weight;
1325 }
1326 }
1327 else
1328 {
1329 unsigned x, y;
1330 int *idst;
1331 unsigned idst_offset;
1332
1333 weight = (weight * 65536) | (weight & 0xffff);
1334 idst = (int *) range;
1335 idst_offset = offset [state + level * wfa->states]
1336 / 2;
1337 for (x = width >> 1; x; x--)
1338 *idst++ = weight & 0xfffefffe;
1339 idst += (offset [state + level * wfa->states]
1340 - width) / 2;
1341
1342 for (y = height - 1; y; y--)
1343 {
1344 memcpy (idst, idst - idst_offset,
1345 width * sizeof (word_t));
1346 idst += idst_offset;
1347 }
1348 }
1349 }
1350 edge = 1;
1351 }
1352 else
1353 edge = 0;
1354
1355 /*
1356 * Add remaining weighted domain images to current range
1357 */
1358 for (; isedge (domain = wfa->into[state][label][edge]);
1359 edge++)
1360 {
1361 if (domain != 0)
1362 {
1363 word_t *src;
1364 unsigned src_offset;
1365 int weight;
1366
1367 src = simg [domain + (level - 1) * wfa->states];
1368 src_offset = offset [domain + ((level - 1)
1369 * wfa->states)] - width;
1370 weight = wfa->int_weight [state][label][edge];
1371
1372 if (width == 1) /* can't add two-pixels in a row */
1373 {
1374 word_t *dst;
1375 unsigned dst_offset;
1376
1377 dst = range;
1378 dst_offset = offset [state + level * wfa->states]
1379 - width;
1380
1381 #ifdef HAVE_SIGNED_SHIFT
1382 *dst++ += ((weight * (int) *src++) >> 10) << 1;
1383 #else /* not HAVE_SIGNED_SHIFT */
1384 *dst++ += ((weight * (int) *src++) / 1024) * 2;
1385 #endif /* not HAVE_SIGNED_SHIFT */
1386 if (height == 2)
1387 {
1388 src += src_offset;
1389 dst += dst_offset;
1390 #ifdef HAVE_SIGNED_SHIFT
1391 *dst++ += ((weight * (int) *src++) >> 10) << 1;
1392 #else /* not HAVE_SIGNED_SHIFT */
1393 *dst++ += ((weight * (int) *src++) / 1024) * 2;
1394 #endif /* not HAVE_SIGNED_SHIFT */
1395 }
1396 }
1397 else
1398 {
1399 int *idst;
1400 unsigned idst_offset;
1401 unsigned y;
1402
1403 idst = (int *) range;
1404 idst_offset = (offset [state + level * wfa->states]
1405 - width) / 2;
1406
1407 for (y = height; y; y--)
1408 {
1409 int *comp_dst = idst + (width >> 1);
1410
1411 for (; idst != comp_dst;)
1412 {
1413 int tmp; /* temp. value of adjacent pixels */
1414 #ifdef HAVE_SIGNED_SHIFT
1415 # if BYTE_ORDER == LITTLE_ENDIAN
1416 tmp = (((weight * (int) src [1]) >> 10) << 17)
1417 | (((weight * (int) src [0]) >> 9)
1418 & 0xfffe);
1419 # else
1420 tmp = (((weight * (int)src [0]) >> 10) << 17)
1421 | (((weight * (int)src [1]) >> 9)
1422 & 0xfffe);
1423 # endif
1424 #else /* not HAVE_SIGNED_SHIFT */
1425 # if BYTE_ORDER == LITTLE_ENDIAN
1426 tmp = (((weight * (int) src [1]) / 1024)
1427 * 131072)
1428 | (((weight * (int) src [0])/ 512)
1429 & 0xfffe);
1430 # else
1431 tmp = (((weight * (int) src [0]) / 1024)
1432 * 131072)
1433 | (((weight * (int) src [1])/ 512)
1434 & 0xfffe);
1435 # endif
1436 #endif /* not HAVE_SIGNED_SHIFT */
1437 src += 2;
1438 *idst = (*idst + tmp) & 0xfffefffe;
1439 idst++;
1440 }
1441 src += src_offset;
1442 idst += idst_offset;
1443 }
1444 }
1445 }
1446 else
1447 {
1448 int weight = (int) (wfa->weight[state][label][edge]
1449 * wfa->final_distribution[0]
1450 * 8 + .5) * 2;
1451 /*
1452 * Range needs domain 0
1453 * (the constant function f(x, y) = 1),
1454 * hence a faster algorithm is used.
1455 */
1456 if (width == 1) /* can't add two-pixels in a row */
1457 {
1458 word_t *dst;
1459 unsigned dst_offset;
1460
1461 dst = range;
1462 dst_offset = offset [state + level * wfa->states]
1463 - width;
1464
1465 *dst++ += weight;
1466 if (height == 2)
1467 {
1468 dst += dst_offset;
1469 *dst++ += weight;
1470 }
1471 }
1472 else
1473 {
1474 int *idst;
1475 unsigned idst_offset;
1476 unsigned y;
1477
1478 weight = (weight * 65536) | (weight & 0xffff);
1479 idst = (int *) range;
1480 idst_offset = (offset [state + level * wfa->states]
1481 - width) /2;
1482
1483 for (y = height; y; y--)
1484 {
1485 int *comp_dst = idst + (width >> 1);
1486
1487 for (; idst != comp_dst; )
1488 {
1489 *idst = (*idst + weight) & 0xfffefffe;
1490 idst++;
1491 }
1492 idst += idst_offset;
1493 }
1494 }
1495 }
1496 }
1497 }
1498 }
1499 }
1500
1501 static word_t *
1502 duplicate_state_image (const word_t *domain, unsigned offset, unsigned level)
1503 /*
1504 * Allocate new memory block 'pixels' and copy pixel values of 'domain'
1505 * (size and pixel offset are given by 'level' and 'offset')
1506 * to the lock 'pixels'.
1507 *
1508 * Return value:
1509 * pointer to the new domain block
1510 */
1511 {
1512 word_t *dst, *pixels;
1513 int y, n;
1514
1515 dst = pixels = Calloc (size_of_level (level), sizeof (word_t));
1516
1517 if (domain)
1518 for (y = height_of_level (level); y; y--)
1519 {
1520 memcpy (dst, domain, width_of_level (level) * sizeof (word_t));
1521 dst += width_of_level (level);
1522 domain += offset;
1523 }
1524 else /* state 0 */
1525 for (n = size_of_level (level); n; n--)
1526 *dst++ = (int) (128 * 8 + .5) * 2;
1527
1528 return pixels;
1529 }