Commit c5387bca authored by Davis King's avatar Davis King

Changed code around a little to avoid problems in gcc when used with mingw. One of the

problems was that assign_pixel() would sometimes give the wrong output when optimizations
were enabled.  This seems to be a bug in mingw's gcc and these code changes avoid triggering
it.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403812
parent b6f6ab72
......@@ -8,6 +8,7 @@
#include "../gui_widgets.h"
#include "../unicode.h"
#include "../smart_pointers_thread_safe.h"
#include "../uintn.h"
#include <map>
......@@ -34,12 +35,7 @@ namespace nativefont
namespace font_renderer
{
#if defined(WIN32) && !defined(__MINGW32__)
typedef BYTE uint8_t;
typedef WORD uint16_t;
typedef DWORD uint32_t;
#endif
typedef uint8_t byte;
typedef dlib::uint8 byte;
#ifdef WIN32
......@@ -62,7 +58,7 @@ namespace nativefont
typedef char type_t;
};
template <> struct size2inner_trait<2>{
typedef uint16_t type_t;
typedef dlib::uint16 type_t;
};
template <> struct size2inner_trait<4>{
typedef dlib::unichar type_t;
......
......@@ -432,106 +432,70 @@ namespace dlib
namespace assign_pixel_helpers
{
enum
{
grayscale = 1,
rgb,
hsi,
rgb_alpha
};
template <
typename P1,
typename P2,
int p1_type = static_switch<
pixel_traits<P1>::grayscale,
pixel_traits<P1>::rgb,
pixel_traits<P1>::hsi,
pixel_traits<P1>::rgb_alpha >::value,
int p2_type = static_switch<
pixel_traits<P2>::grayscale,
pixel_traits<P2>::rgb,
pixel_traits<P2>::hsi,
pixel_traits<P2>::rgb_alpha >::value
>
struct helper;
// -----------------------------
// all the same kind
template < typename P >
struct helper<P,P,grayscale,grayscale>
{
static void assign(P& dest, const P& src)
typename enable_if_c<pixel_traits<P>::grayscale>::type
assign(P& dest, const P& src)
{
dest = src;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,grayscale,grayscale>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::grayscale>::type
assign(P1& dest, const P2& src)
{
if (src <= pixel_traits<P1>::max())
dest = static_cast<P1>(src);
else
dest = static_cast<P1>(pixel_traits<P1>::max());
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb,rgb>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::rgb>::type
assign(P1& dest, const P2& src)
{
dest.red = src.red;
dest.green = src.green;
dest.blue = src.blue;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb_alpha,rgb_alpha>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::rgb_alpha>::type
assign(P1& dest, const P2& src)
{
dest.red = src.red;
dest.green = src.green;
dest.blue = src.blue;
dest.alpha = src.alpha;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,hsi,hsi>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::hsi>::type
assign(P1& dest, const P2& src)
{
dest.h = src.h;
dest.s = src.s;
dest.i = src.i;
}
};
// -----------------------------
// dest is a grayscale
template < typename P1, typename P2 >
struct helper<P1,P2,grayscale,rgb>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb>::type
assign(P1& dest, const P2& src)
{
dest = static_cast<P1>((static_cast<unsigned int>(src.red) +
static_cast<unsigned int>(src.green) +
static_cast<unsigned int>(src.blue))/3);
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,grayscale,rgb_alpha>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::rgb_alpha>::type
assign(P1& dest, const P2& src)
{
const unsigned char avg = static_cast<unsigned char>((static_cast<unsigned int>(src.red) +
......@@ -562,16 +526,13 @@ namespace dlib
dest += static_cast<unsigned char>(temp&0xFF);
}
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,grayscale,hsi>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::grayscale && pixel_traits<P2>::hsi>::type
assign(P1& dest, const P2& src)
{
dest = static_cast<P1>(src.i);
}
};
// -----------------------------
......@@ -676,20 +637,17 @@ namespace dlib
// dest is a color rgb_pixel
template < typename P1 >
struct helper<P1,unsigned char,rgb,grayscale>
{
static void assign(P1& dest, const unsigned char& src)
typename enable_if_c<pixel_traits<P1>::rgb>::type
assign(P1& dest, const unsigned char& src)
{
dest.red = src;
dest.green = src;
dest.blue = src;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb,grayscale>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::grayscale>::type
assign(P1& dest, const P2& src)
{
unsigned char p;
if (src <= 255)
......@@ -701,12 +659,10 @@ namespace dlib
dest.green = p;
dest.blue = p;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb,rgb_alpha>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::rgb_alpha>::type
assign(P1& dest, const P2& src)
{
if (src.alpha == 255)
{
......@@ -744,12 +700,10 @@ namespace dlib
dest.blue += static_cast<unsigned char>(temp_b&0xFF);
}
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb,hsi>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb && pixel_traits<P2>::hsi>::type
assign(P1& dest, const P2& src)
{
COLOUR c;
HSL h;
......@@ -763,28 +717,24 @@ namespace dlib
dest.green = static_cast<unsigned char>(c.g*255.0);
dest.blue = static_cast<unsigned char>(c.b*255.0);
}
};
// -----------------------------
// dest is a color rgb_alpha_pixel
template < typename P1 >
struct helper<P1,unsigned char,rgb_alpha,grayscale>
{
static void assign(P1& dest, const unsigned char& src)
typename enable_if_c<pixel_traits<P1>::rgb_alpha>::type
assign(P1& dest, const unsigned char& src)
{
dest.red = src;
dest.green = src;
dest.blue = src;
dest.alpha = 255;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb_alpha,grayscale>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::grayscale>::type
assign(P1& dest, const P2& src)
{
unsigned char p;
if (src <= 255)
......@@ -797,24 +747,20 @@ namespace dlib
dest.blue = p;
dest.alpha = 255;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb_alpha,rgb>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::rgb>::type
assign(P1& dest, const P2& src)
{
dest.red = src.red;
dest.green = src.green;
dest.blue = src.blue;
dest.alpha = 255;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,rgb_alpha,hsi>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::rgb_alpha && pixel_traits<P2>::hsi>::type
assign(P1& dest, const P2& src)
{
COLOUR c;
HSL h;
......@@ -829,27 +775,23 @@ namespace dlib
dest.blue = static_cast<unsigned char>(c.b*255.0);
dest.alpha = 255;
}
};
// -----------------------------
// dest is an hsi pixel
template < typename P1>
struct helper<P1,unsigned char,hsi,grayscale>
{
static void assign(P1& dest, const unsigned char& src)
typename enable_if_c<pixel_traits<P1>::hsi>::type
assign(P1& dest, const unsigned char& src)
{
dest.h = 0;
dest.s = 0;
dest.i = src;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,hsi,grayscale>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::grayscale>::type
assign(P1& dest, const P2& src)
{
dest.h = 0;
dest.s = 0;
......@@ -858,30 +800,10 @@ namespace dlib
else
dest.i = 255;
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,hsi,rgb_alpha>
{
static void assign(P1& dest, const P2& src)
{
rgb_pixel temp;
// convert target hsi pixel to rgb
helper<rgb_pixel,P1>::assign(temp,dest);
// now assign the rgb_alpha value to our temp rgb pixel
helper<rgb_pixel,P2>::assign(temp,src);
// now we can just go assign the new rgb value to the
// hsi pixel
helper<P1,rgb_pixel>::assign(dest,temp);
}
};
template < typename P1, typename P2 >
struct helper<P1,P2,hsi,rgb>
{
static void assign(P1& dest, const P2& src)
typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::rgb>::type
assign(P1& dest, const P2& src)
{
COLOUR c1;
HSL c2;
......@@ -894,7 +816,23 @@ namespace dlib
dest.s = static_cast<unsigned char>(c2.s*255.0);
dest.i = static_cast<unsigned char>(c2.l*255.0);
}
};
template < typename P1, typename P2 >
typename enable_if_c<pixel_traits<P1>::hsi && pixel_traits<P2>::rgb_alpha>::type
assign(P1& dest, const P2& src)
{
rgb_pixel temp;
// convert target hsi pixel to rgb
assign(temp,dest);
// now assign the rgb_alpha value to our temp rgb pixel
assign(temp,src);
// now we can just go assign the new rgb value to the
// hsi pixel
assign(dest,temp);
}
}
// -----------------------------
......@@ -903,7 +841,7 @@ namespace dlib
inline void assign_pixel (
P1& dest,
const P2& src
) { assign_pixel_helpers::helper<P1,P2>::assign(dest,src); }
) { assign_pixel_helpers::assign(dest,src); }
template < typename P1>
inline void assign_pixel (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment