+ // Reduce the image size if its too big to make it fit on the surface
+ int oldw = w, oldh = h;
+ unsigned long *olddata = data;
+ if (w > sfw) w = sfw;
+ if (h > sfh) h = sfh;
+ unsigned long newdata[w*h];
+ if (w < oldw || h < oldh) {
+ double dx = oldw / (double)w;
+ double dy = oldh / (double)h;
+ double px = 0.0;
+ double py = 0.0;
+ int iy = 0;
+ for (i = 0, c = 0, e = w*h; i < e; ++i) {
+ newdata[i] = olddata[(int)px + iy];
+ if (++c >= w) {
+ c = 0;
+ px = 0;
+ py += dy;
+ iy = (int)py * oldw;
+ } else
+ px += dx;
+ }
+ data = newdata;
+ }