mirror of
https://github.com/amyinspace/MagicSetEditor2.git
synced 2026-06-10 21:06:59 -04:00
Be explicit about type of angles: either Radians or Degrees.
Angles are always doubles. Internally use radians as much as possible. git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@1605 0fc631ac-6414-0410-93d0-97cfa31319b6
This commit is contained in:
@@ -36,7 +36,7 @@ class GeneratedImage : public ScriptValue {
|
||||
mutable int width, height; ///< Width to force the image to, or 0 to keep the width of the input
|
||||
///< In that case, width and height will be later set to the actual size
|
||||
double zoom; ///< Zoom factor to use, when width=height=0
|
||||
int angle; ///< Angle to rotate image by afterwards
|
||||
Radians angle; ///< Angle to rotate image by afterwards
|
||||
PreserveAspect preserve_aspect;
|
||||
bool saturate;
|
||||
Package* package; ///< Package to load images from
|
||||
@@ -264,13 +264,13 @@ class FlipImageVertical : public SimpleFilterImage {
|
||||
/// Rotate an image
|
||||
class RotateImage : public SimpleFilterImage {
|
||||
public:
|
||||
inline RotateImage(const GeneratedImageP& image, double angle)
|
||||
inline RotateImage(const GeneratedImageP& image, Radians angle)
|
||||
: SimpleFilterImage(image), angle(angle)
|
||||
{}
|
||||
virtual Image generate(const Options& opt) const;
|
||||
virtual bool operator == (const GeneratedImage& that) const;
|
||||
private:
|
||||
double angle;
|
||||
Radians angle;
|
||||
};
|
||||
|
||||
// ----------------------------------------------------------------------------- : EnlargeImage
|
||||
|
||||
+3
-16
@@ -16,6 +16,7 @@
|
||||
|
||||
#include <util/prec.hpp>
|
||||
#include <util/real_point.hpp>
|
||||
#include <util/angle.hpp>
|
||||
#include <gfx/color.hpp>
|
||||
|
||||
// ----------------------------------------------------------------------------- : Resampling
|
||||
@@ -52,29 +53,15 @@ void sharp_resample_and_clip(const Image& img_in, Image& img_out, wxRect rect, i
|
||||
* rect = rectangle to draw in (a rectangle somewhere around pos)
|
||||
* stretch = amount to stretch in the direction of the text after drawing
|
||||
*/
|
||||
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, AColor color, const String& text, int blur_radius = 0, int repeat = 1);
|
||||
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, Radians angle, AColor color, const String& text, int blur_radius = 0, int repeat = 1);
|
||||
|
||||
// scaling factor to use when drawing resampled text
|
||||
extern const int text_scaling;
|
||||
|
||||
// ----------------------------------------------------------------------------- : Image rotation
|
||||
|
||||
/// Is an angle a {0,90,180,270}?
|
||||
inline bool straight(int angle) { return angle % 90 == 0; }
|
||||
|
||||
/// Is an angle sideways (90 or 270 degrees)?
|
||||
inline bool sideways(int angle) {
|
||||
int a = (angle + 3600) % 180;
|
||||
return (a > 45 && a < 135);
|
||||
}
|
||||
|
||||
/// Convert radians to degrees
|
||||
inline double rad_to_deg(double rad) { return rad * (180.0 / M_PI); }
|
||||
/// Convert degrees to radians
|
||||
inline double deg_to_rad(double deg) { return deg * (M_PI / 180.0); }
|
||||
|
||||
/// Rotates an image counter clockwise
|
||||
Image rotate_image(const Image& image, double angle);
|
||||
Image rotate_image(const Image& image, Radians angle);
|
||||
|
||||
/// Flip an image horizontally
|
||||
Image flip_image_horizontal(const Image& image);
|
||||
|
||||
@@ -165,7 +165,7 @@ void blur_image_alpha(Image& img) {
|
||||
|
||||
// Draw text by first drawing it using a larger font and then downsampling it
|
||||
// optionally rotated by an angle
|
||||
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, int angle, AColor color, const String& text, int blur_radius, int repeat) {
|
||||
void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, double stretch, Radians angle, AColor color, const String& text, int blur_radius, int repeat) {
|
||||
// transparent text can be ignored
|
||||
if (color.alpha == 0) return;
|
||||
// enlarge slightly; some fonts are larger then the GetTextExtent tells us (especially italic fonts)
|
||||
@@ -183,11 +183,11 @@ void draw_resampled_text(DC& dc, const RealPoint& pos, const RealRect& rect, dou
|
||||
// now draw the text
|
||||
mdc.SetFont(dc.GetFont());
|
||||
mdc.SetTextForeground(*wxWHITE);
|
||||
mdc.DrawRotatedText(text, xsub, ysub, angle);
|
||||
mdc.DrawRotatedText(text, xsub, ysub, rad_to_deg(angle));
|
||||
// get image
|
||||
mdc.SelectObject(wxNullBitmap);
|
||||
// step 2. sample down
|
||||
double ca = fabs(cos(deg_to_rad(angle))), sa = fabs(sin(deg_to_rad(angle)));
|
||||
double ca = fabs(cos(angle)), sa = fabs(sin(angle));
|
||||
w += int(w * (stretch - 1) * ca); // GCC makes annoying conversion warnings if *= is used here.
|
||||
h += int(h * (stretch - 1) * sa);
|
||||
Image img_small(w, h, false);
|
||||
|
||||
+10
-14
@@ -46,7 +46,7 @@ Image rotate_image_impl(Image img) {
|
||||
// ----------------------------------------------------------------------------- : Rotations
|
||||
|
||||
// Function object to handle rotation
|
||||
struct Rotate90 {
|
||||
struct Rotate90deg {
|
||||
/// Init a rotated image, where the source is w * h pixels
|
||||
inline static void init(Image& img, UInt w, UInt h) {
|
||||
img.Create(h, w, false);
|
||||
@@ -59,7 +59,7 @@ struct Rotate90 {
|
||||
}
|
||||
};
|
||||
|
||||
struct Rotate180 {
|
||||
struct Rotate180deg {
|
||||
inline static void init(Image& img, UInt w, UInt h) {
|
||||
img.Create(w, h, false);
|
||||
}
|
||||
@@ -70,7 +70,7 @@ struct Rotate180 {
|
||||
}
|
||||
};
|
||||
|
||||
struct Rotate270 {
|
||||
struct Rotate270deg {
|
||||
inline static void init(Image& img, UInt w, UInt h) {
|
||||
img.Create(h, w, false);
|
||||
}
|
||||
@@ -83,19 +83,15 @@ struct Rotate270 {
|
||||
|
||||
// ----------------------------------------------------------------------------- : Interface
|
||||
|
||||
double almost_equal(double x, double y) {
|
||||
return fabs(x-y) < 1e-6;
|
||||
}
|
||||
|
||||
Image rotate_image(const Image& image, double angle) {
|
||||
double a = fmod(angle, 360);
|
||||
if (almost_equal(a, 0)) return image;
|
||||
if (almost_equal(a, 90)) return rotate_image_impl<Rotate90> (image);
|
||||
if (almost_equal(a,180)) return rotate_image_impl<Rotate180>(image);
|
||||
if (almost_equal(a,270)) return rotate_image_impl<Rotate270>(image);
|
||||
Image rotate_image(const Image& image, Radians angle) {
|
||||
double a = constrain_radians(angle);
|
||||
if (is_rad0(a)) return image;
|
||||
if (is_rad90(a)) return rotate_image_impl<Rotate90deg> (image);
|
||||
if (is_rad180(a)) return rotate_image_impl<Rotate180deg>(image);
|
||||
if (is_rad270(a)) return rotate_image_impl<Rotate270deg>(image);
|
||||
else {
|
||||
if (!image.HasAlpha()) const_cast<Image&>(image).InitAlpha();
|
||||
return image.Rotate(angle * M_PI / 180, wxPoint(0,0));
|
||||
return image.Rotate(angle, wxPoint(0,0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user