+void image::postprocess()
+{
+ if (1 == tile_s_ && 1 == tile_t_) return;
+
+ SDL_Surface* src = SDL_CreateRGBSurfaceFrom
+ (
+ pixels_,
+ width_,
+ height_,
+ depth_,
+ pitch_,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ 0x000000FF,
+ 0x0000FF00,
+ 0x00FF0000,
+ 0xFF000000
+#else
+ 0xFF000000,
+ 0x00FF0000,
+ 0x0000FF00,
+ 0x000000FF
+#endif
+ );
+ SDL_Surface* dst = SDL_CreateRGBSurface(
+ SDL_SWSURFACE,
+ higher_power_of_two(width_),
+ higher_power_of_two(height_),
+ depth_,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ 0x000000FF,
+ 0x0000FF00,
+ 0x00FF0000,
+ 0xFF000000
+#else
+ 0xFF000000,
+ 0x00FF0000,
+ 0x0000FF00,
+ 0x000000FF
+#endif
+ );
+ ASSERT(src && dst);
+
+ SDL_SetAlpha(src, 0, 128);
+ SDL_SetAlpha(dst, 0, 128);
+
+ int src_tilew = src->w / tile_s_;
+ int src_tileh = src->h / tile_t_;
+ int dst_tilew = dst->w / tile_s_;
+ int dst_tileh = dst->h / tile_t_;
+
+ for (int t = 0; t < tile_t_; ++t) for (int s = 0; s < tile_s_; ++s)
+ {
+ SDL_Rect srcrect;
+ SDL_Rect dstrect;
+ srcrect.x = s * src_tilew;
+ srcrect.y = t * src_tileh;
+ srcrect.w = src_tilew;
+ srcrect.h = src_tileh;
+ dstrect.x = s * dst_tilew + (src_tilew / 2);
+ dstrect.y = t * dst_tileh + (src_tileh / 2);
+ log_warning("SRC", srcrect.x, srcrect.y, srcrect.w, srcrect.h);
+ log_warning("DST", dstrect.x, dstrect.y);
+ SDL_BlitSurface(src, &srcrect, dst, &dstrect);
+
+ srcrect.x = s * src_tilew;
+ srcrect.y = t * src_tileh;
+ srcrect.w = 1;
+ srcrect.h = src_tileh;
+ dstrect.y = t * dst_tileh + (src_tileh / 2);
+ for (int x = s * dst_tilew + (src_tilew / 2) - 1; s * dst_tilew <= x; --x)
+ {
+ dstrect.x = x;
+ SDL_BlitSurface(src, &srcrect, dst, &dstrect);
+ }
+
+ srcrect.x = (s + 1) * src_tilew - 1;
+ srcrect.y = t * src_tileh;
+ srcrect.w = 1;
+ srcrect.h = src_tileh;
+ dstrect.y = t * dst_tileh + (src_tileh / 2);
+ for (int x = (s + 1) * dst_tilew - (src_tilew / 2); x < (s + 1) * dst_tilew; ++x)
+ {
+ dstrect.x = x;
+ SDL_BlitSurface(src, &srcrect, dst, &dstrect);
+ }
+
+ srcrect.x = s * src_tilew;
+ srcrect.y = t * src_tileh;
+ srcrect.w = src_tilew;
+ srcrect.h = 1;
+ dstrect.x = s * dst_tilew + (src_tilew / 2);
+ for (int y = t * dst_tileh + (src_tileh / 2) - 1; t * dst_tileh <= y; --y)
+ {
+ dstrect.y = y;
+ SDL_BlitSurface(src, &srcrect, dst, &dstrect);
+ }
+
+ srcrect.x = s * src_tilew;
+ srcrect.y = (t + 1) * src_tileh - 1;
+ srcrect.w = src_tilew;
+ srcrect.h = 1;
+ dstrect.x = s * dst_tilew + (src_tilew / 2);
+ for (int y = (t + 1) * dst_tileh - (src_tileh / 2); y < (t + 1) * dst_tileh; ++y)
+ {
+ dstrect.y = y;
+ SDL_BlitSurface(src, &srcrect, dst, &dstrect);
+ }
+ }
+ SDL_FreeSurface(src);
+
+ char* pixels = new char[dst->w * dst->pitch];
+ SDL_Surface* finaldst = SDL_CreateRGBSurfaceFrom
+ (
+ pixels,
+ dst->w,
+ dst->h,
+ depth_,
+ dst->pitch,
+#if SDL_BYTEORDER == SDL_LIL_ENDIAN
+ 0x000000FF,
+ 0x0000FF00,
+ 0x00FF0000,
+ 0xFF000000
+#else
+ 0xFF000000,
+ 0x00FF0000,
+ 0x0000FF00,
+ 0x000000FF
+#endif
+ );
+ SDL_BlitSurface(dst, 0, finaldst, 0);
+
+ //SDL_SaveBMP(dst, (stlplus::basename_part(path) + ".bmp").c_str());
+ SDL_FreeSurface(dst);
+
+ width_ = finaldst->w;
+ height_ = finaldst->h;
+ pitch_ = finaldst->pitch;
+
+ SDL_FreeSurface(finaldst);
+
+ delete[] pixels_;
+ pixels_ = pixels;
+}
+
+
+void image::fix_uv(std::vector<vector2>& p) const
+{
+ vector2 mid(SCALAR(0.0), SCALAR(0.0));
+ for (int i = 0; i < p.size(); ++i)
+ {
+ mid[0] += p[i][0];
+ mid[1] += p[i][1];
+ }
+ mid[0] /= p.size();
+ mid[1] /= p.size();
+ mid[0] *= tile_s_;
+ mid[1] *= tile_t_;
+ mid[0] = std::floor(mid[0]);
+ mid[1] = std::floor(mid[1]);
+ log_warning("midpoint:", mid);
+ scalar s = mid[0];
+ scalar t = mid[1];
+
+ scalar src_width = width_ >> 1;
+ scalar src_height = height_ >> 1;
+ int src_tilew = src_width / tile_s_;
+ int src_tileh = src_height / tile_t_;
+ int dst_tilew = width_ / tile_s_;
+ int dst_tileh = height_ / tile_t_;
+
+ for (int i = 0; i < p.size(); ++i)
+ {
+ //scalar s = p[i][0] * src_width;
+ scalar x = s * dst_tilew + (src_tilew / 2) + (p[i][0] * src_width - s * src_tilew);
+ p[i][0] = x / width_;
+ //scalar t = p[i][1] * src_height;
+ scalar y = t * dst_tileh + (src_tileh / 2) + (p[i][1] * src_height - t * src_tileh);;
+ p[i][1] = y / height_;
+ }
+}
+
+