Note that any tile returned by this function must be freed */
| 177 | |
| 178 | /* Note that any tile returned by this function must be freed */ |
| 179 | struct TileImage * |
| 180 | stretch_tile(const struct TileImage *inp_tile, |
| 181 | unsigned out_width, unsigned out_height) |
| 182 | { |
| 183 | unsigned x_scale_inp, x_scale_out, y_scale_inp, y_scale_out; |
| 184 | unsigned divisor; |
| 185 | unsigned size; |
| 186 | struct TileImage *out_tile; |
| 187 | unsigned x_inp, y_inp, x2, y2, x_out, y_out; |
| 188 | unsigned pos; |
| 189 | |
| 190 | /* Derive the scale factors */ |
| 191 | divisor = gcd(out_width, inp_tile->width); |
| 192 | x_scale_inp = inp_tile->width / divisor; |
| 193 | x_scale_out = out_width / divisor; |
| 194 | divisor = gcd(out_height, inp_tile->height); |
| 195 | y_scale_inp = inp_tile->height / divisor; |
| 196 | y_scale_out = out_height / divisor; |
| 197 | |
| 198 | /* Derive the stretched tile */ |
| 199 | out_tile = (struct TileImage *) alloc(sizeof(struct TileImage)); |
| 200 | out_tile->width = out_width; |
| 201 | out_tile->height = out_height; |
| 202 | size = out_width * out_height; |
| 203 | if (inp_tile->pixels != NULL) { |
| 204 | out_tile->pixels = (struct Pixel *) alloc(size * sizeof(struct Pixel)); |
| 205 | divisor = x_scale_inp * y_scale_inp; |
| 206 | for (y_out = 0; y_out < out_height; ++y_out) { |
| 207 | for (x_out = 0; x_out < out_width; ++x_out) { |
| 208 | unsigned r, g, b, a; |
| 209 | |
| 210 | /* Derive output pixels by blending input pixels */ |
| 211 | r = 0; |
| 212 | g = 0; |
| 213 | b = 0; |
| 214 | a = 0; |
| 215 | for (y2 = 0; y2 < y_scale_inp; ++y2) { |
| 216 | y_inp = (y_out * y_scale_inp + y2) / y_scale_out; |
| 217 | for (x2 = 0; x2 < x_scale_inp; ++x2) { |
| 218 | x_inp = (x_out * x_scale_inp + x2) / x_scale_out; |
| 219 | pos = y_inp * inp_tile->width + x_inp; |
| 220 | r += inp_tile->pixels[pos].r; |
| 221 | g += inp_tile->pixels[pos].g; |
| 222 | b += inp_tile->pixels[pos].b; |
| 223 | a += inp_tile->pixels[pos].a; |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | pos = y_out * out_width + x_out; |
| 228 | out_tile->pixels[pos].r = r / divisor; |
| 229 | out_tile->pixels[pos].g = g / divisor; |
| 230 | out_tile->pixels[pos].b = b / divisor; |
| 231 | out_tile->pixels[pos].a = a / divisor; |
| 232 | } |
| 233 | } |
| 234 | } else { |
| 235 | out_tile->pixels = NULL; |
| 236 | } |