public boolean resize(int w, int h)
    {
        if(this.w == w && this.h == h) return true;

        FormatTemplate best_format = null;
        int best_err = 0;
        int best_scale = 1;
        for(int scale = 1; scale < 8; ) {
            for(FormatTemplate st : format_templates) {
                int err = st.cost(w, h, scale) + scale;
                if(best_format == null || err < best_err) {
                    best_format = st;
                    best_err = err;
                    best_scale = scale;
                }
            }

            if(scale_type == SCALE_NONE) scale = 100000;
            else if(scale_type == SCALE_POW2) scale *= 2;
            else scale++;
        }

        if(best_format == null)
            return false;

        this.w = w;
        this.h = h;
        set_format(best_format, best_scale);
        return true;
    }