-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.h
108 lines (90 loc) · 2.22 KB
/
image.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#pragma once
namespace miki
{
struct loadgraph_exception : std::exception{};
namespace detail
{
struct image_key
{
std::string name;
int allnum,xnum,ynum,xsize,ysize;
friend bool operator==( image_key const& lhs, image_key const& rhs )
{
return ( lhs.ysize == rhs.ysize )
&& ( lhs.xsize == rhs.xsize )
&& ( lhs.allnum == rhs.allnum )
&& ( lhs.xnum == rhs.xnum )
&& ( lhs.ynum == rhs.ynum )
&& ( lhs.name == rhs.name );
}
};
struct imagekey_hash{
size_t operator()( image_key const& k )const
{
size_t h = 0;
boost::hash_combine( h, k.name );
boost::hash_combine( h, k.xnum );
boost::hash_combine( h, k.ynum );
boost::hash_combine( h, k.xsize );
boost::hash_combine( h, k.ysize );
boost::hash_combine( h, k.allnum );
return h;
}
};
struct image_impl
{
explicit image_impl(image_key const& imgkey)
:gs( imgkey.allnum )
{
BOOST_ASSERT( ( imgkey.allnum == 1 && imgkey.xnum == 1 && imgkey.ynum == 1 && imgkey.xsize == 1 && imgkey.ysize == 1 )
|| ( imgkey.allnum > 1 ) );
if ( imgkey.allnum == 1 ){
gs.front() = LoadGraph( imgkey.name.c_str() );
BOOST_ASSERT( gs.front() != -1 );
}
else{
#ifndef NDEBUG
int loadret =
#endif
LoadDivGraph( imgkey.name.c_str()
, imgkey.allnum, imgkey.xnum, imgkey.ynum, imgkey.xsize, imgkey.ysize
, gs.data() );
#ifndef NDEBUG
BOOST_ASSERT(loadret != -1);
#endif
}
}
~image_impl()
{
boost::for_each( gs,[]( int const h ){ DeleteGraph( h ); } );
}
std::vector<int> gs;
};
} // namespace detail
typedef
boost::flyweights::flyweight
< boost::flyweights::key_value< detail::image_key
, detail::image_impl >
, boost::flyweights::hashed_factory< detail::imagekey_hash > >
image
;
inline image make_image( std::string const& imgfile
, int allnum
, int xdiv
, int ydiv
, int xsize
, int ysize )
{
detail::image_key tmp = { imgfile,allnum,xdiv,ydiv,xsize,ysize };
return image( tmp );
}
inline image make_image( std::string const& imgfile )
{
return make_image( imgfile, 1, 1, 1, 1, 1 );
}
inline int to_draw( miki::image const& img, size_t const i = 0U )
{
BOOST_ASSERT( img.get().gs.size() > i );
return img.get().gs[i];
}
} // namespace miki