Debian Codegraph @

converter/other/fiasco/codec/tiling.c

raw
1 /*
2 * tiling.c: Subimage permutation
3 *
4 * Written by: Ullrich Hafner
5 *
6 * This file is part of FIASCO (Fractal Image And Sequence COdec)
7 * Copyright (C) 1994-2000 Ullrich Hafner
8 */
9
10 /*
11 * $Date: 2000/06/14 20:50:51 $
12 * $Author: hafner $
13 * $Revision: 5.1 $
14 * $State: Exp $
15 */
16
17 #include "config.h"
18
19 #include <stdbool.h>
20 #include <stdlib.h>
21
22 #include "pm_c_util.h"
23
24 #include "types.h"
25 #include "macros.h"
26 #include "error.h"
27
28 #include "image.h"
29 #include "misc.h"
30 #include "wfalib.h"
31 #include "tiling.h"
32
33
34
35 typedef struct VarList {
36 int address; /* bintree address */
37 real_t variance; /* variance of tile */
38 } VarList;
39
40 #ifndef LITERAL_FN_DEF_MATCH
41 static qsort_comparison_fn cmpincvar;
42 #endif
43
44 static int
45 cmpincvar(const void * const value1P,
46 const void * const value2P) {
47 /*----------------------------------------------------------------------------
48 Sort by increasing variances (quicksort sorting function)
49 -----------------------------------------------------------------------------*/
50 return
51 ((VarList *)value1P)->variance - ((VarList *)value2P)->variance;
52 }
53
54
55
56 #ifndef LITERAL_FN_DEF_MATCH
57 static qsort_comparison_fn cmpdecvar;
58 #endif
59
60 static int
61 cmpdecvar(const void * const value1P,
62 const void * const value2P) {
63 /*----------------------------------------------------------------------------
64 Sort by decreasing variances (quicksort sorting function).
65 -----------------------------------------------------------------------------*/
66 return
67 ((VarList *)value2P)->variance - ((VarList *)value1P)->variance;
68 }
69
70
71
72 tiling_t *
73 new_tiling(fiasco_tiling_e const method,
74 unsigned int const reqTilingExponent,
75 unsigned int const imageLevel,
76 bool const isVideo) {
77 /*----------------------------------------------------------------------------
78 Construct an image tiling object.
79
80 'method' defines the tiling method (spiral or variance; ascending or
81 descending).
82
83 'reqTilingExponent' is the requested tiling exponent, though if it is too
84 large for the 'imageLevel', we issue a warning and make the object say
85 tiling exponent 6. Also, if we're doing video ('isVideo'), make it
86 zero and issue a warning.
87
88 Return value is pointer to newly malloced object.
89 -----------------------------------------------------------------------------*/
90 tiling_t * const tilingP = Calloc(1, sizeof(tiling_t));
91
92 if (isVideo && reqTilingExponent > 0)
93 warning(_("Image tiling valid only with still image compression."));
94
95 if (isVideo)
96 tilingP->exponent = 0;
97 else {
98 if ((int)imageLevel - (int)reqTilingExponent < 6) {
99 tilingP->exponent = 6;
100 warning(_("Image tiles must be at least 8x8 pixels large.\n"
101 "Setting tiling size to 8x8 pixels."));
102 } else
103 tilingP->exponent = reqTilingExponent;
104 }
105
106 return tilingP;
107 }
108
109
110
111 void
112 free_tiling (tiling_t * const tilingP) {
113 /*----------------------------------------------------------------------------
114 Destroy tiling object *tilingP.
115 -----------------------------------------------------------------------------*/
116 if (tilingP->vorder)
117 Free(tilingP->vorder);
118
119 Free(tilingP);
120 }
121
122
123
124 static void
125 performTilingVariance(const image_t * const imageP,
126 tiling_t * const tilingP) {
127
128 bool_t * tileIsValid; /* malloced array */
129 /* tileIsValid[i] means i is in valid range */
130
131 unsigned int const tileCt = 1 << tilingP->exponent;
132 /* number of image tiles */
133
134 tileIsValid = Calloc(tileCt, sizeof(bool_t));
135
136 unsigned int const lx = log2 (imageP->width - 1) + 1; /* x level */
137 unsigned int const ly = log2 (imageP->height - 1) + 1; /* y level */
138 unsigned int const level = MAX(lx, ly) * 2 - ((ly == lx + 1) ? 1 : 0);
139 VarList * const varList = Calloc(tileCt, sizeof(VarList)); /* array */
140
141 unsigned int address; /* bintree address of tile */
142 unsigned int number; /* number of image tiles */
143
144 /* Compute variances of image tiles */
145
146 for (number = 0, address = 0; address < tileCt; ++address) {
147 unsigned width, height; /* size of image tile */
148 unsigned x0, y0; /* NW corner of image tile */
149
150 locate_subimage (level, level - tilingP->exponent, address,
151 &x0, &y0, &width, &height);
152 if (x0 < imageP->width && y0 < imageP->height) {
153 /* valid range */
154 if (x0 + width > imageP->width) /* outside image area */
155 width = imageP->width - x0;
156 if (y0 + height > imageP->height) /* outside image area */
157 height = imageP->height - y0;
158
159 varList[number].variance =
160 variance(imageP->pixels [GRAY], x0, y0,
161 width, height, imageP->width);
162 varList[number].address = address;
163 ++number;
164 tileIsValid[address] = YES;
165 } else
166 tileIsValid[address] = NO;
167 }
168
169 /* Sort image tiles according to sign of 'tilingP->exp' */
170 if (tilingP->method == FIASCO_TILING_VARIANCE_DSC)
171 qsort(varList, number, sizeof(VarList), cmpdecvar);
172 else
173 qsort(varList, number, sizeof(VarList), cmpincvar);
174
175 for (number = 0, address = 0; address < tileCt; ++address)
176 if (tileIsValid[address]) {
177 tilingP->vorder[address] = varList[number].address;
178 ++number;
179 debug_message("tile number %d has original address %d",
180 number, tilingP->vorder[address]);
181 } else
182 tilingP->vorder[address] = -1;
183
184 Free(varList);
185 }
186
187
188
189 void
190 perform_tiling(const image_t * const imageP,
191 tiling_t * const tilingP) {
192 /*----------------------------------------------------------------------------
193 Compute image tiling permutation, updating it in *tilingP.
194
195 The image is split into 2**'tilingP->exponent' tiles.
196
197 Depending on 'tilingP->method', we use one of the following algorithms:
198
199 "VARIANCE_ASC" : Tiles are sorted by variance.
200 The first tile has the lowest variance
201 "VARIANCE_DSC" : Tiles are sorted by variance.
202 The first tile has the largest variance
203 "SPIRAL_ASC" : Tiles are sorted like a spiral starting
204 in the middle of the image.
205 "SPIRAL_DSC" : Tiles are sorted like a spiral starting
206 in the upper left corner.
207 -----------------------------------------------------------------------------*/
208 if (tilingP->exponent) {
209 unsigned int const tileCt = 1 << tilingP->exponent;
210 /* number of image tiles */
211
212 tilingP->vorder = Calloc(tileCt, sizeof(int));
213
214 if (tilingP->method == FIASCO_TILING_VARIANCE_ASC
215 || tilingP->method == FIASCO_TILING_VARIANCE_DSC) {
216
217 performTilingVariance(imageP, tilingP);
218 } else if (tilingP->method == FIASCO_TILING_SPIRAL_DSC
219 || tilingP->method == FIASCO_TILING_SPIRAL_ASC) {
220 compute_spiral(tilingP->vorder, imageP->width, imageP->height,
221 tilingP->exponent,
222 tilingP->method == FIASCO_TILING_SPIRAL_ASC);
223 } else {
224 warning("We do not know the tiling method.\n"
225 "Skipping image tiling step.");
226 tilingP->exponent = 0;
227 }
228 }
229 }
230