| 40 | } |
| 41 | |
| 42 | void resize_image(const Image* src, Image* dst, ResizeMethod method, const Palette* pal, const RgbMap* rgbmap, color_t maskColor) |
| 43 | { |
| 44 | switch (method) { |
| 45 | |
| 46 | // TODO optimize this |
| 47 | case RESIZE_METHOD_NEAREST_NEIGHBOR: { |
| 48 | ASSERT(src->pixelFormat() == dst->pixelFormat()); |
| 49 | |
| 50 | switch (src->pixelFormat()) { |
| 51 | case IMAGE_RGB: resize_image_nearest<RgbTraits>(src, dst); break; |
| 52 | case IMAGE_GRAYSCALE: resize_image_nearest<GrayscaleTraits>(src, dst); break; |
| 53 | case IMAGE_INDEXED: resize_image_nearest<IndexedTraits>(src, dst); break; |
| 54 | case IMAGE_BITMAP: resize_image_nearest<BitmapTraits>(src, dst); break; |
| 55 | } |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | // TODO optimize this |
| 60 | case RESIZE_METHOD_BILINEAR: { |
| 61 | uint32_t color[4], dst_color = 0; |
| 62 | double u, v, du, dv; |
| 63 | int u_floor, u_floor2; |
| 64 | int v_floor, v_floor2; |
| 65 | int x, y; |
| 66 | |
| 67 | u = v = 0.0; |
| 68 | du = (src->width()-1) * 1.0 / (dst->width()-1); |
| 69 | dv = (src->height()-1) * 1.0 / (dst->height()-1); |
| 70 | for (y=0; y<dst->height(); ++y) { |
| 71 | for (x=0; x<dst->width(); ++x) { |
| 72 | u_floor = (int)floor(u); |
| 73 | v_floor = (int)floor(v); |
| 74 | |
| 75 | if (u_floor > src->width()-1) { |
| 76 | u_floor = src->width()-1; |
| 77 | u_floor2 = src->width()-1; |
| 78 | } |
| 79 | else if (u_floor == src->width()-1) |
| 80 | u_floor2 = u_floor; |
| 81 | else |
| 82 | u_floor2 = u_floor+1; |
| 83 | |
| 84 | if (v_floor > src->height()-1) { |
| 85 | v_floor = src->height()-1; |
| 86 | v_floor2 = src->height()-1; |
| 87 | } |
| 88 | else if (v_floor == src->height()-1) |
| 89 | v_floor2 = v_floor; |
| 90 | else |
| 91 | v_floor2 = v_floor+1; |
| 92 | |
| 93 | // get the four colors |
| 94 | color[0] = src->getPixel(u_floor, v_floor); |
| 95 | color[1] = src->getPixel(u_floor2, v_floor); |
| 96 | color[2] = src->getPixel(u_floor, v_floor2); |
| 97 | color[3] = src->getPixel(u_floor2, v_floor2); |
| 98 | |
| 99 | // calculate the interpolated color |